Regex for valid java identifier [duplicate] - javascript

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

Related

Regex Matches too much? [duplicate]

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]*)\)\]

Replace symbols in a string that are not letters/digits/underscore with an underscore (Javascript or Typescript)? [duplicate]

This question already has answers here:
Remove not alphanumeric characters from string
(10 answers)
Closed 2 years ago.
I have a random string that comes from a data source that I do not control.
My upstream system has a requirement for such IDs: "Field names must begin with a letter and can contain only letters, digits, or underscore ('_')".
How, in Typescript/Javascript (possibly, with lodash) can I replace all string symbols that is not a letter/digit/underscore to an underscore?
const input = '8~3m_E!P$m%S^wx_-';
input.replace(/[\W]/gm, '_');
// output: 8_3m_E_P_m_S_wx__
Note: This doesn't cover this part of the requirement: 'Field names must begin with a letter'. However, because you didn't ask about it specifically, I assumed you already had it covered.

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

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

Write a Regex that accepts string of alphanumeric characters except certain words [duplicate]

This question already has answers here:
A regular expression to exclude a word/string
(7 answers)
Closed 7 years ago.
I am trying to write a regex that accepts url with pathname of the form
/exmaple/XXXXX
Here,XXXXX can be a string of alphanumeric keywords but it should not be "create" or "add".
I have created the regex for accepting alphanumeric but I am unable to find a way to add exceptions of "create" or "add".
The negative lookahead is your friend, foo(?!pattern) means foo will not match if pattern matches immediately after foo. Like other lookarounds, negative lookaheads are not capture groups.
/\/example\/(?!create|add)[A-Za-z0-9]+/

Categories