Home > Blockchain >  Search for a pattern in string Js
Search for a pattern in string Js

Time:12-22

I'm trying to code something, I tried but that doesn't work, so I have a string and I want my code to search for a pattern in it. For example, if 'hello, this is a test' I want to search in this string the 'etet' pattern and the first iteration. In that case, it will be: 'hello, this is a test' and after, the other characters will be replaced by '-' like so: '-e-----t----------e-t'. I tried regex :

string = string.replace(/[^eEtT]/g, '-')

but it gives every letters, and not the only pattern. Thanks for helping.

CodePudding user response:

You might use 2 capture groups, first capturing e and then the next t In the callback of replace, check if there is a capture group.

(e)[^t]*(t)|.

Regex demo

Replace with group 1 and group 2, and repeat adding _ for the number of characters that matched in between.

const regex = /(e)[^t]*(t)|./gi;
const s = "hello, this is a test.";
const result = s.replace(regex, (m, g1, g2) =>
  g1 ? g1   "-".repeat(m.length - 2)   g2 : "-".repeat(m.length)
);
console.log(result);

CodePudding user response:

I honestly don't see a practical use for this, but if you want it, sure I guess. Something like this might help:

function stringSearch(string, search, caseSensitive=true) {
    if(caseSensitive==false) {
        string = string.toLowerCase();
        search = search.toLowerCase();
    }

    let cache = ""; // this will store the characters we have found
    let complete = false; // whether we found the search string or not

    let pos = 0; // current position in search

    for(i=0;i<string.length;i  ) { // loop through every character in the string
        if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
            cache  = search[pos]; // if so, add the character to the cache
            pos  ; // add 1 to the position
            if(cache == search) { // check if the cache is equal to the search string
                complete = true; // set complete to true
                break; // break the loop
            }
        }
    }

    return complete;
}


// examples:
console.log("'helolo' has 'hello': " stringSearch('helolo', 'hello')); // true
console.log("'helo' has 'hello': " stringSearch('helo', 'hello')) // false
console.log("'HELLO' has 'hello': " stringSearch('HELLO', 'hello')); // false
console.log("'HELLO' has 'hello' (incase sensitive): " stringSearch('HELLO', 'hello', false)); // true

The way this works is pretty simple. It has a cache variable. That variable will hold every character that matches the search string.

So, it loops through every character in the big string. There is then another variable which stores the current position in the search string. If the current character in the big string matches the current character in the search string, it adds it to the cache variable and adds one to the search string's position. It then runs an if statement to see if the cache is equal to the search string. If so, it returns true. If you wanted, you can make the function return the cache instead of a boolean.

If you wanted the dashes, you can use this code:

function stringSearch(string, search, caseSensitive=true) {
    if(caseSensitive==false) {
        string = string.toLowerCase();
        search = search.toLowerCase();
    }

    let cache = ""; // this will store the characters we have found
    let complete = false; // whether we found the search string or not
    let returnValue = "";

    let pos = 0; // current position in search

    for(i=0;i<string.length;i  ) { // loop through every character in the string
        if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
            cache  = search[pos]; // if so, add the character to the cache
            returnValue  = search[pos]; // add the character to the return value
            pos  ; // add 1 to the position
            if(cache == search) { // check if the cache is equal to the search string
                complete = true;
                break; // break the loop
            }
        } else {
            returnValue  = "-"; // add a - to the return value
        }
    }

    if(complete) {
        return returnValue; // return the result
    } else {
        return;
    }
}


// examples:
console.log("'helolo' has 'hello': " stringSearch('helolo', 'hello')); // true
console.log("'helo' has 'hello': " stringSearch('helo', 'hello')) // false
console.log("'HELLO' has 'hello': " stringSearch('HELLO', 'hello')); // false
console.log("'HELLO' has 'hello' (incase sensitive): " stringSearch('HELLO', 'hello', false)); // true
console.log("'hello, this is a test' has 'etet': " stringSearch("hello, this is a test", "etet"));

It works pretty much the same. The only difference is an added returnValue variable which stores the dashes and text. Every time a character is added to cache, it's also added to returnValue. BUT, if a value ISN'T added to cache, a - is added to returnValue.

The reason this doesn't catch the middle 't' is because when it goes over that 't,' it's still looking for an 'e.'

  • Related