1

This is the regular expression that i have:

new RegExp("\\b" + synonym.toLowerCase() + "\\b", "g").test(modSearchString.toLowerCase())

So the scenario is that modSearchString contains a string of characters/words. It is checking if the modSearchString contains the synonym and if it does to do something. Now, it has been working really well. However, it doesn't seem to work if the synonym contains a plus sign. Most of these synonyms don't have a plus sign, but there will be a possibility that some will. How can I also account for the plus sign?

EDIT

I want it so that the phrase in modSearchString, matches exactly, with the synonym passed. So, if the synonym is "ThisPass+", and modSearchstring = 'Test for ThisPass++", it should fail. However, it should pass if modSearchstring = 'Test for ThisPass+". The same should go with if the synonym was "ThisPass". It should pass if modSearchstring = 'Test for ThisPass" and fail if modSearchstring = 'Test for ThisPass+".

The first regex i have works well in finding an the search string contains that synonym. However, that would only work if it's word characters. Since, the synonym may contain a + sign it'll probably not work well.

pmb88
  • 147
  • 2
  • 11
  • 1
    You need to escape the plus sign, check out the top answer here: http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – Dan May 06 '16 at 19:04
  • `+` is not a word character, so eg `\b\+` will match, if there is a letter/digit/underscore directly in front of it, but not if there is a space, comma or whatever. You might want to check for `\B\+` if there is a plus at the start or `\+\B` if it is at the end. – Sebastian Proske May 06 '16 at 19:04
  • If you mean the `synonym` ends or starts with a plus sign, you can't use word boundaries, Use `(?:^|\W)` and `(?:$|\W)` – Wiktor Stribiżew May 06 '16 at 19:06
  • Do you have some example? – Washington Guedes May 06 '16 at 19:12
  • @WiktorStribiżew how would that be written. Is it something like this `new RegExp("(?:^|\W)" + synonym + "(?:$|\W)", "ig")` – pmb88 May 07 '16 at 18:52
  • 1
    `new RegExp("(?:^|\\W)" + synonym.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?:$|\\W)")` – Wiktor Stribiżew May 07 '16 at 19:16
  • @WiktorStribiżew This looks to work. However, I do see an issue. If the synonym is "ThisPass+" and modSearchString contains "ThisPass++", it would return true. The same would happen if the synonym is "ThisPass" and the modsearchstring contains "ThisPass+" – pmb88 May 07 '16 at 19:35
  • Update the question with the new requirements and provide test cases. – Wiktor Stribiżew May 07 '16 at 20:00
  • @WiktorStribiżew I edited above and provided a few examples. – pmb88 May 08 '16 at 16:44
  • I do not think it is clear: what is considered a valid word boundary? What is the expected behavior for `ThisPass` vs. `ThisPass, ThisPass; ThisPass.`? See [`(?:^|\W)\bThisPass\b(?!\w|\+)`](https://regex101.com/r/cM5mL5/1). Also, consider [`(?:^|\s)ThisPass\+(?=\s|$)`](https://regex101.com/r/cM5mL5/2). – Wiktor Stribiżew May 08 '16 at 17:36
  • That means you need `new RegExp("(?:^|\\s)" + synonym + "(?=\\s|$)")` – Wiktor Stribiżew May 08 '16 at 21:25
  • @WiktorStribiżew The way it would work is that if the synonym is this ThisPass it should only pass if the search string contains ThisPass exactly. So in your examples if the term contained the following, ThisPass, ThisPass;, ThisPass., it should fail because there special characters right after it. It looks your second regex seems to work and looks to meet what I am looking for. The one you posted `new RegExp("(?:^|\\s)" + synonym + "(?=\\s|$)")` looks to fit that. – pmb88 May 08 '16 at 21:26
  • @WiktorStribiżew . Thank you for taking the time for you to answer my query. Can you post it below as an answer so I can mark it as so? I would like to also understand better what you are doing with the regular expression. So `(?:^|\s`)` means that it may begin at the start of the string or it may have a space before it, and `(?=\s|$)` means that it must have either a space or be at the end, correct? – pmb88 May 08 '16 at 21:49

2 Answers2

1

It seems you need

new RegExp("(?:^|\\s)" + synonym + "(?=\\s|$)")

The regex demo can be checked here.

The first group (?:^|\s) just matches either the start of a string (^) or a whitespace (\s), and the trailing lookahead will just check (will not consume as it is a zero width assertion) if there is a whitespace or the end of the string ($).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

If you just need to check if one string contains the other you can do this:

var modSearchString = "foo bar";
var synonym = "bar";

if(modSearchString.indexOf(synonym) > -1){
    //modSearchString contains synonym
}else{
    //modSearchString does not contain synonym
}