What do the question marks like ?: and ?! mean in Javascript regexp? [duplicate] - javascript

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What is a non-capturing group in regular expressions?
(18 answers)
What does ?! mean?
(3 answers)
Closed 2 years ago.
Like the regexp in this one? What does it match?
document.getElementById("MyElement").className =
document.getElementById("MyElement").className.replace
( /(?:^|\s)MyClass(?!\S)/ , '' )

?: means make the capturing group a non capturing group, i.e. don't include its match as a back-reference. This is often done to increase performance and de-clutter the back-references when a capturing group is necessary to use the | operator.
In your example, it is being used to allow the or (|) of the start of the string ^ or whitespace (\s). Since the author of this code doesn't care about what it matched, they have made it a non capturing group.
?! is the negative lookahead. The regex will only match if the capturing group does not match.
In this example, the author wants to ensure the character after MyClass is not a whitespace character (\S).
It is somewhat possible the author of this code could have used word boundaries instead (\b).

The regular expression (?:^|\s) is a non-capturing group that matches either the start of the line or a whitespace character.
The regular expression (?!\S) is a negative lookahead assertion which succeeds either at the end of the string, or else when the next character is a whitespace character.

Related

Non-greed regex misunderstanding, /\/.*?$/ act like greed [duplicate]

This question already has answers here:
Regex lazy vs greedy confusion
(2 answers)
Why does a simple .*? non-greedy regex greedily include additional characters before a match?
(3 answers)
Closed 3 years ago.
I'm trying this in javascript
/\/.*?$/.exec('foo/bar/tar')[0]
I was expecting to get /tar as result but getting /bar/tar. As far as I understand non-greed regex would take the smallest match.
I'm circumventing this with myvar.split('/').reverse()[0] but I couldn't understand what is going wrong with the regex.
There is nothing wrong with the regex but the pattern \/.*?$ matches from the first forward slash until the end of the string non greedy.
The dot matches any character except a newline and does not take a forward slash into account, so that will result in /bar/tar.
If you want to match /tar, you could match a forward slash, followed by not matching anymore forward slashes using a negated character class and then assert the end of the string.
\/[^\/]+$
Pattern demo
console.log(/\/[^\/]+$/.exec('foo/bar/tar')[0]);

Why does this regex not exclude hyphens or brackets? [duplicate]

This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
Match exact string
(3 answers)
Closed 5 years ago.
Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^ character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_] or any character a-z and A-Z as well as and underscore _. Then the string can have anything that includes [A-Za-z\d_] which is any alphanumeric character and an underscore. Then I use the * to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [ or a hyphen -. Why does this expression not exclude these characters
Extra info
I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false
It's actually matching the first 2 letters("va"), that's why it's true.
To match the whole phrase, your reg expression should have "$" at the end:
"^[A-Za-z_][A-Za-z\d_]*$"
Your regex matches the part of the string that does not contain the bracket, because your're missing the $ anchor that would (together with ^) force it to match the whole string. Use
const reg = /^[A-Za-z_][A-Za-z\d_]*$/g
// ^
function variableName(name) {
return reg.test(name)
}
console.log(variableName("va[riable0"))

Can you explain me this specific line of Javascript? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
var newstr= str.replace(/[\W_]/g, ''); //replace all non-alfanumeric blank
This was the code i tried while removing the non-alfanumeric characters from a string in Javascript.
Please explain what is the difference between </[\W_]/g> and </\w+/g> and </\w/g>.
If we have /[\W_]/g any of this special symbols in str object replace the empty("") value.
\w : 1 word character. It's equivalent to [A-Za-z0-9_]
\w+ : 1 or more word characters.
\W : 1 non-word character. It's equivalent to [^\w] or [^A-Za-z0-9_]
So \W is anything that's not a letter, not a digit and not an underscore.
[\W_] : A character class with non-word characters and underscore.
It's equivalent to [^A-Za-z0-9]
Note that with a global replace to nothing, the code str.replace(/\W/g,'') will give the same result as str.replace(/\W+/g,''). The former will just replace them 1 character at a time, while the latter will replace groups of 1 or more characters at the time.
More info about the regex syntax can be found in this old post
Or you could just copy&paste your regex in an online regex tester.
For example regexr or regex101
Then check out the explain, or hover your mouse pointer over the pattern to get hints about each part of the pattern.

using regex to see if string contains this word only, not within another word [duplicate]

This question already has an answer here:
Match and replace whole words in javascript
(1 answer)
Closed 8 years ago.
I have a regex that matches these strings in a string; however, it is matching non-words ( parts-of-words ) as well.
For example city is matched as it contains it. However, I want only the string it to be matched it if it the only characters between whitespace. So it or he would match, but not city or where.
Here is the regex ( pretty basic and simple ): they|he|she|her|him|them|it.
How can I get it to match these words if the word is only this?
Use word boundaries to denote the beginning and ending of a word.
http://www.regular-expressions.info/wordboundaries.html
So your regex would become something on the order of:
\b(they|he|she|her|him|them|it)\b
Check it out
It should be noted that this regular expression won't match words containing apostrophes, e.g. can't, won't, etc. For a discussion of this, see the following Stackoverflow post:
How do you use the Java word boundary with apostrophes?
Try to put an word boundary before the words,
(?:\bthey\b|\bhe\b|\bshe\b|\bher\b|\bhim\b|\bthem\b|\bit\b)
Explanation:
(?:...) # Non captuaring groups
\b # Word boundary(It matches between a word character and a non word character)
DEMO

regex remove white space after text [duplicate]

This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 4 years ago.
From this regex,
text.replace(/^\s+|\s+$/g,"").replace(/ +/g,' ')
how do I remove the regex just for trailing white space?
I am new to regex and did some research but I'm not able to understand the pattern.
/^\s+|\s+$/g means
^ // match the beginning of the string
\s+ // match one or more whitespace characters
| // OR if the previous expression does not match (i.e. alternation)
\s+ // match one or more whitespace characters
$ // match the end of the string
The g modifier indicates to repeat the matching until no match is found anymore.
So if you want to remove the part the matches whitespace characters at the end of the string, remove the |\s+$ part (and the g flag since ^\s+ can only match at one position anyway - at the beginning of the string).
Useful resources to learn regular expressions:
http://www.regular-expressions.info/
Regex in JavaScript (since this seems to be JavaScript).

Categories

Resources