Does the Javascript regex pattern \W include spaces? - javascript

I am using this expression: /\W+/g to match all characters that are not numbers, letters and spaces. It seems to be including spaces. How would I build a regex that did not include spaces?

/[^a-z0-9\s]+/ig
Explanation:
[^ Character class which matches characters NOT in the following class
a-z All lowercase letters of the alphabet
0-9 All numbers
\s Whitespace characters
] End of the character class
i Case-insensitivity to match uppercase letters

A more accurate wording for \W is any Non-Alphanumeric character.
\s is for Any Whitespace.
So, it would be something like this:
[^\s]

\W means "non-word characters", the inverse of \w, so it will match spaces as well. I'm a bit surprised it doesn't match numbers, though.

Related

Which symbol matches "underline" in Regex in Js

str = "The stor-+)_y is someth12ing that tr##ee3 de124scrib%^&ing
becom5es life7 4 difficult";
console.log(str.replace(/\W/g,''));
Guys, have written this RegEx for matching all non alphanumeric characters, but cannot select 'underline' ?
As I know, \d is for all digits
\s for whitespace... so, what letter stand for underline?
To match all non-alphanumeric characters, \W is not enough since matches the same text as [^a-zA-Z0-9_] does. To match _ with your regex, add _ and \W to a character class:
str = "The stor-+)_y is someth12ing that tr##ee3 de124scrib%^&ing becom5es life7 4 difficult";
console.log(str.replace(/[\W_]+/g,''));
Since you are removing, it is advisable to quantify with + (to remove 1+ consecutive occureences in one go).

How to can I get all special character, where is not SPACE, using Regex?

When I use \W in Regex, it will get all special character, but I wan not get Space.
How to can I get all special character using Regex, where it is not Space, in javascript?
You can use negated character class instead:
[^\w\s]
This will match a character that is not a word character and not a white-space.
RegEx Demo
You could simply use [^\s\w] which will return all characters that are not space nor letters
Regex101

My regular expression isn't matching correctly

I'm using the following regular expression to match one or more special characters for a password strength test.
if (password.match(/\W+/)) points++;
This doesn't seem to match the underscore '_' as a special character. Why is this and how can I fix it?
It is because \W is the same as [^\w], while \w contains a-z, A-Z, 0-9, and _ as well.
In order to fix it just add _ character separately:
if (password.match(/[\W_]+/)) points++;
\W (uppercase) means not \w, so anything except word characters.
Word characters (\w) includes letters, digits, and underscore.
Perhaps you should use /[^a-z0-9]+/i to match non-letters.
Are you sure you don't want the \w? The \W is the negation of \w.
\w matches (letters, digits, and underscores), so \W does NOT match letters, digits, and underscores. See here: http://www.regular-expressions.info/reference.html
The match fails because underscore is treated as a word character. From the MDN documentation for \W:
Matches any non-word character. Equivalent to [^A-Za-z0-9_]
You can fix this by grouping underscore and \W:
if (password.match(/[\W_]+/)) points++;
A regex tool such as Javascript Regex Tester can be especially helpful for debugging this sort of thing.

How to make the below regex to accept any special character

/^[^ ]([\w- \.\\\/&#]+)[^ ]$/,
I have the above regex. I want to make sure it accepts all special characters but i don't want to specify the entire special character listsuch as [\w- \.\\\/&#!##$&]. How can we make sure the above regex accepts all special characters
[^\w\s] matches any non-alphanumeric and non-whitespace character.
\S matches any non-whitespace character.
. matches any character except newlines.
[\S\s] matches any character in a JavaScript regex.
Since you've got \w and a space in there already, you must want all of the ASCII characters except control characters. That would be:
[ -~]
...or any character whose code point is in the range U+0020 (space) to U+007E (tilde). But it looks like you want to make sure the first and last characters are not whitespace. In fact, looking at your previous question, I'll assume you want only letters or digits in those positions. This would work:
/^[A-Za-z0-9][ -~]*[A-Za-z0-9]$/
...but that requires the string to be at least two characters long. To allow for a single-character string, change it to this:
/^[A-Za-z0-9](?:[ -~]*[A-Za-z0-9])?$/
In other words, if there's only one character, it must be a letter or digit. If there are two or more characters, the first and last must letters or digits, while the rest can be any printing character--i.e., a letter, a digit, a "special" (punctuation) character, or a space.
Note that this only matches ASCII characters, not accented Latin letters like  or ë, or symbols from other alphabets or writing systems.
. matches any character except for newline.

JavaScript special characters/regular expressions

I'm trying to learn from reading Mozilla documentation for regular expressions, but there's one thing I don't get. For the special character \s it gives the following example
/\s\w*/ matches ' bar' in "foo bar."
I understand that \s is the special character for white space, but why is there a w* in the example?
doesn't /\s/ also match ' bar' in "foo bar."?
What's with the w*?
/\s\w*/ is whitespace character followed by 0 or more word characters.
/\s/ would only find the whitespace in the example.
\w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]).
It's a character escape.
\w is all word characters (letters, digits, and underscores)
Check this link for more documentation on such shorthand

Categories