1

I am mapping through a list of words (with random characters too) and the regex doesn't seem to work and ends up throwing an error. I basically have a const variable (called content) I want to search to see if there are certain words in the content variable.

so I have

if (list.words.map(lword=> {
    const re = new RegExp("/" + lword+ "\\/g");
    if (re.test(content)) {
        return true;
    }
}

But that just fails out and doesn't catch anything. I get a Nothing to repeat error. Specifically: Uncaught SyntaxError: Invalid regular expression: //Lu(*\/g/: Nothing to repeat

I'm not sure how to search through content to see if it contains lword.

Barmar
  • 741,623
  • 53
  • 500
  • 612
thatdevop
  • 819
  • 2
  • 8
  • 19
  • Related: [Is there a regex escape function in javascript](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript) – CRice May 21 '18 at 20:14
  • The way you use RegExp is wrong https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – epascarello May 21 '18 at 20:15
  • @epascarello it's not totally wrong, but if you pass a value to create a Regexp you need to escape that value, in order to replace special characters. – muecas May 21 '18 at 20:18
  • @muecas The leading and trailing `/` should not be there, the flags should not be there.... And yes, it has escaping issues.... – epascarello May 21 '18 at 20:21
  • @epascarello thats the part of "not totally wrong"; he needs to fix that ans escape the passed value, to replace special characters. – muecas May 21 '18 at 20:22
  • Using `map` with `if` makes no sense. `map` will return an array, and arrays are always truthy. Did you mean `list.words.some(...)`? – Barmar May 21 '18 at 20:24

1 Answers1

0

When you use new RegExp() you don't put the delimiters and modifiers in the string, just the expression. The modifiers go in an optional second argument.

const re = new RegExp(lword, "g");

If you want to treat lword as a literal string to search for, rather than a regular expression pattern, you shouldn't use RegExp in the first place. Just search for it with indexOf():

const list = {
  words: ["this", "some", "words"]
};

const content = "There are some word here";

if (list.words.some(lword => content.indexOf(lword) != -1)) {
  console.log("words were found");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Barmar I tried that right now and I guess it got me closer but i still got ```Uncaught SyntaxError: Invalid regular expression: /*damn/: Nothing to repeat``` – thatdevop May 21 '18 at 20:19
  • You need to escape the regex https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – muecas May 21 '18 at 20:20
  • @devOpGuy Why are you using `RegExp` if you don't want to process it as a regular expression pattern? – Barmar May 21 '18 at 20:20
  • @Barmar I don't see another way I can get a string(i trim it and all) and see if a substring is contained within it. for example if I had the string `Development` and my substring was `elo`, how would I be able to see if that is in there? so I have an array of substrings so I'm trying to figure out if they exist in the main string. @muecas i'll take a look at that now! – thatdevop May 21 '18 at 20:22
  • @devOpGuy I've added that to the answer. – Barmar May 21 '18 at 20:22
  • I have a check of ''' if (list.words.indexOf(context) >= 0) { return true; }''' I can't find it. am i doing the search in reverse? – thatdevop May 21 '18 at 20:23
  • This stills not solve the `Uncaught SyntaxError: Invalid regular expression: //Lu(*\/g/: Nothing to repeat` error. – muecas May 21 '18 at 20:24
  • @muecas That's because he has invalid regular expressions in his array. – Barmar May 21 '18 at 20:25
  • The proper answer will be to escape the passed `lword` to replace regex special characters. – muecas May 21 '18 at 20:26
  • @muecas No, the proper solution is to not use `RegExp` if it's not a regular expression. – Barmar May 21 '18 at 20:27
  • @devOpGuy That's not the correct place to use `indexOf`. See my snippet. – Barmar May 21 '18 at 20:32
  • @Barmar That is the correct answer! I tried this but i didn't treat it as a map and i think i got the `indexOf` confused a bit! This totally works, thank you! – thatdevop May 21 '18 at 20:38