This question already has answers here:
How can I match a whole word in JavaScript?
(4 answers)
Closed 5 years ago.
I am trying to match the word ethane (preceded with nothing) while avoiding methane. I've tried this in an online regex tester: /(?<!m)ethane/i (which works), but I get an invalid regex expression error in JavaScript. What am I doing wrong?
You can use RegExp /\bethane\b/ to match "ethane" and not "methane"
var thanes = ["ethane", "methane"];
var re = /\bethane\b/;
thanes.forEach(word => console.log(re.test(word)));
See
Difference between \b and \B in regex
How does \b work when using regular expressions?
Related
This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 4 years ago.
How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.
if (!/\s/.test(character))
console.log('this is not a whitespace');
if (/\S/.test(character))
console.log('this is not a whitespace');
Use the negated set notation in the regex
/[^\s]/
That will match everything that isnt a whitespace.
This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
I'm trying to use this regex pattern to match this value [1]
When I use this regex:
new RegExp("\\["+row+"\\]/g")
it returns me this:
/\[1\]\/g/
instead of this:
/\[1\]/g
I have tried many things but nothing works apparently.
Some help would be appreciated.
Don't put the regex delimiter when you're using the constructor:
new RegExp("\\["+row+"\\]", 'g')
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 5 years ago.
I'm trying to write a regex to blacklist certain words. I'm able to create a whitelist like /^(carrots|onions|corn)$/ but how would I convert that into a blacklist?
Edit: To clarify, I'm matching this blacklist against a whole string. For example "corndog" should be allowed. I want the regex equivalent of blacklistArray.indexOf(word) === -1
Use negative lookahead:
^(?!.*(?:carrots|onions|corn))
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How can I create a regex for that returns true if it has only numbers and '+' basically 0-9 & +. Using javascript or jQuery.
Regex for plus anywhere: /^[0-9+]+$/
Regex for plus only infront: /^\+?[0-9]+$/
What it does:
^ Matches the beginning of the string
[0-9+] Matches 0123456789+
+ Matches one or more
$ Matches the end of the string
Other version:
\+? Matches zero or one plus signs in the front
Maybe try regexr for future regex development.
How to test in code:
function isOnlyNumber(str) {
return /^[0-9+]+$/.test(str);
}
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I have a regular expression that matches strings that start with either the word "db" or the word "admin":
/^\/(db|admin)\//
I'm refactoring my code and my requirements have changed: Now I need the opposite, i.e. a regular expression that matches strings that don't start with one of those two words. Is this possible with regular expressions?
Note: I cannot use JS API - the regular expression is inserted in Express.js's app.all(path, callback) method directly (as the path).
Thanks to Nico for pointing out that JavaScript RegExp has (?!) functionality. The solution seems to be:
/^\/(?!admin|db)/