JavaScript regex for blacklisting words [duplicate] - javascript

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))

Related

how to ignore removing a special character in a string [duplicate]

This question already has answers here:
Regex remove special characters
(4 answers)
Closed 2 years ago.
I am trying to strip all non numbers and this symbol * from a string.
An example would be
Input: "gdfgdf234dg54gf*23oP42"
Output: "23454*2342"
I have the following which removes everything apart from numbers. The issue is that it also removes the *. I am unsure how to ignore one character.
string.replace(/\D/g,'');
I am sure I will get ridiculed on here saying what else have I tried, but have searched for numerous answers on here.
Can anyone help?
Try [^\d*]
Regex Demo
console.log("gdfgdf234dg54gf*23oP42".replace(/[^\d*]/g,''))

Regex pattern for URL string [duplicate]

This question already has answers here:
Regex to match only letters
(20 answers)
Closed 3 years ago.
I need to find regex pattern for url and use regex.test() so only string like this:
http://*.margonem.pl/
so it's exactly like above string and where * must appear and can be string which only contains a-z letters without any signs.
That would be http:\/\/[a-z]+\.margonem\.pl\/:
Matches
http://a.margonem.pl/
http://foo.margonem.pl/
Does not match
http://hello-world.margonem.pl/
http://abcq443435u4531.margonem.pl/

JS: check if a single character is NOT a whitespace [duplicate]

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.

Regex for valid java identifier [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 5 years ago.
I am trying to write a regex for a valid java identifier name: I have tried this:
([a-zA-Z_$][a-zA-Z\d_$]*)
but it is accepting spaces also. However, I didn't allowed space to be considered as a match.
Any idea?
You need to include the start and end markers to enforce that the entire string matches the regex. Otherwise, it passes if any part of the string matches.
^([a-zA-Z_$][a-zA-Z\d_$]*)$

Regex in JavaScript acting differently than elsewhere [duplicate]

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?

Categories