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,''))
Related
This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 2 years ago.
Using regex, I'm trying to check whether there's only 2-4 characters used after a .. At the moment, i got it working to detect when its less than 2 characters but after 4 characters, it still deems it as successful. How can I fix this? This is what I have written down:
/.[a-zA-Z]{2,4}$/
You need to escape the dot (.).
/\.[a-zA-Z]{2,4}$/
. is the control character in RegExp, you should to escape it:
/\.[a-zA-Z]{2,4}$/
And, to ignore case, add the i flag:
/\.[a-z]{2,4}$/i
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]*)\)\]
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:
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_$]*)$
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))