Reputation: 1206
I really have no other way of explaining this however, I need a regular expression to match in javascript.
I have a spam prevention to match a string however, I would like multiple guesses and match those using regex.
As an example, I want to match thiswordhere. To return true, the matches can be:
It must be a standalone word so no spaces and no responses like word word or thisword word
Four possible outcomes. I'm very new to regex and all I could get using regexr is:
/(thisword)[here]/g
Which couldn't do the trick. I'm going to be studying regex a lot these coming months so I would like to see the solution for this example.
Upvotes: 0
Views: 133
Reputation: 24405
To match only that text on a line (or in your string), you can simply add the start ^
and end $
delimiters to your regex:
/^((?:this)?word(?:here)?)$/g
A quick and easy way is to specify those particular outcomes as options:
/(thiswordhere|thisword|wordhere|word)/g
A slightly better option might be to specify that the "word" part is always needed, with "this" optional on the left, and "here" optional on the right:
/((?:this)?word(?:here)?)/g
FYI - your regex is saying match "thisword" literally, followed by any of the four characters "h", "e", "r", "e". What you need to say is "match word, optionally preceded by "this" and optionally followed by "here" (example above).
Upvotes: 2
Reputation: 30995
You can use a regex like this:
\b(?:this)?word(?:here)?\b
Upvotes: 1