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

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.

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

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

Phone/Fax regular expression in JavaScript [duplicate]

This question already has answers here:
RegEx for Phone number in JavaScript
(3 answers)
Closed 8 years ago.
I am a beginner in regex.
I need a regular expression which satisfies following criteria. I tried lot of things but couldn't make it.
total no. of digits can be 10, 11 or 12
expression can except characters like -, (, ), space, /, \
expression can start with any digit/character mentioned above
max length of the expression is 16.
All digits and characters can appear in random order in expression
Can anyone please help me?
this pattern seems to work as requested ^(?=(?:\D*\d){10,12}\D*$)[0-9 \-()\\\/]{1,16}$
Demo
The expression [ 0-9()\-/\\]{10,16} fulfills all your requirements.
[...] is a positive character class definition. A matching character can be one of the characters in the square brackets.
The first character in the square brackets is a space character.
0-9 defines all digits (from charcter 0 to character 9). \d could be also used for any digit.
( and ) are also valid characters.
The character - has a special meaning in square brackets as you can see on 0-9 and therefore must be escaped in square brackets by the backslash character when it should be interpreted as literal character.
The slash is the next character. Please note that also the slash must be escaped with a backslash when using this regular expression in a JavaScript RegExp object.
And the last character with the square brackets is the backslash character which must be always escaped with one more backslash as it is the escape character if the backslash should be interpreted as literal character.
{10,16} ... means that the preceding expression must be positively applied on a string for at least 10 but not more than 16 characters.
But you should really search for expressions matching phone numbers in WWW as this is a very often needed expression. You should not reinvent the wheel respectively the expression.
I recommend using http://regexlib.com for your RegEx needs. Very good site with tons of RegEx's that you can browse on. I also recommend http://regex101.com for testing regular expressions. Has a very great tool to help you build/modify/test your expressions.

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

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

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.

Categories

Resources