Regex Matches too much? [duplicate] - javascript

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 2 years ago.
Not actually too sure what to title this question.
I'm attempting to match only whats between $[( and )] each time.
However the regex I'm using is matching the first $[( and last )] in the entire string instead of each individual occurrence within a string.
Each occurrence could have literally anything around it.
The contents could be even as complex as $[(("$[username]").substr(1))]
So the contents should be possible to be anything too.
You can see a demo of this regex here:
https://regex101.com/r/Wr1e6D/1
and the attached screenshot.

If you know that inside the parenthesis will be alphanumeric characters, you use the following:
\$\[\(([\w\d]*)\)\]

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

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_$]*)$

JavaScript regex for blacklisting words [duplicate]

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

regex extract last occurrence ignoring new line [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 7 years ago.
Considering a string like:
"#foo #foo#foo#foo #foo\n
foofoofoo\n
foo #bar"
I try for 2 days to extract the last #/# occurrence so here, the # before 'bar'. For now, i have something like this [##](?!.*[##]) which seems to work except when user insert new lines in there.
Can someone give me some tips please?
You can use this lookhahead regex:
/[##](?![\s\S]*[##])/
RegEx Demo
(?![\s\S]*[##]) is the negative lookahead that asserts there is no # or # ahead of current position in any line. [\s\S] matches any character including newline.

Categories