Javascript regex for symbol but not space - javascript

I want to test a string for a symbol (e.g. $%^&*) so I use the following regex that works well:
/[\W+]/.test(string)
However, a space is also matched with this regex. What I really want is to test for a symbol but not a space. I'm trying the following code, but a space is still matched:
/[\W\S+]/.test(string)
Is there a better way to do this?

Since the whitespaces are included in \W, you need to use a negated character class:
[^\w\s]
However, you must clearly define what you call a "symbol" since this character class include for example accentued letters and all out of the ascii range.

\W is the equivalent of [^A-Za-z0-9_], meaning "any character except these." So you can use [^A-Za-z0-9_ ] (note the space at the end) to exclude spaces.

You may use the inverse shorthand character class for \W (which is \w) and use a negation in the character class:
/[^\w\s]+/.test(string)
Your regex [\W\S+] is also matching literal + as it is part of a character class. I think you need to place it outside the class to match 1 or more characters.

This regex may help you: [^\s\w]
https://regex101.com/r/jO4uU6/3

You may try this also.
(?!\s)\W
This would match any non-word character but not of a space.
/(?:(?!\s)\W)+/.test(string)
ie, match one or more non-word characters but not of a space.
DEMO

Related

javascript regular expression allow name with one space and special Alphabets

how to write regular expression allow name with one space and special Alphabets?
I tried with this [a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)* but not working for me,
example string Björk Guðmundsdóttir
You may try something along these lines:
^(?!.*[ ].*[ ])[ A-Za-zÀ-ÖØ-öø-ÿ]+$
The first negative lookahead asserts that we do not find two spaces in the name. This implies that at most one space is present (or no spaces at all). Then, we match any number of alphabets, with most accented letters included. Spaces can also be matched, but the lookahead would already ensure that at most one space can be present.
Demo
Use this one:
[a-zA-Z\u00C0-\u00ff]*[ ]{1}[a-zA-Z\u00C0-\u00ff]*
Answer from other question

RegEx works in tester not on site using JavaScript

I'm trying to write a RegEx that returns true if the string starts with / or http: and only allows alpha numeric characters, the dash and underscore. Any white space and any other special characters should fire a false response when tested.
Below works fine (except that it allows special characters, I have not figured out how to do that yet) when tested at https://www.regex101.com/#javascript. Unfortunately returns false when I implement it in my site and test it with /products/homedecor/tablecloths. What am I doing wrong and is there a better regEx to use that would accomplish my goals?
^(\\/|(?:http:))\S+[a-zA-Z0-9-_]+$
Keep unescaped hyphen at first or at last position in character class:
^(\/|(?:http:))[/.a-zA-Z0-9_-]+$
Or even simpler:
^(\/|http:)[/\w.-]+$
Since \w is same as [a-zA-Z0-9_]
To match URL you may need to match DOT and forward slash as well.
Just remove the \S+ from your regex and put the hyphen inside the character class at the first or at the last. Note that \S+ matches any non-space characters (including non-word characters).
^(\/|http:)[a-zA-Z0-9_-]+$

How can I check if string has particular word in javascript

Now, I know I can calculate if a string contains particular substring.Using this:
if(str.indexOf("substr") > -1){
}
Having my substring 'GRE' I want to match for my autocomplete list:
GRE:Math
GRE-Math
But I don't want to match:
CONGRES
and I particularly need to match:
NON-WORD-CHARGRENON-WORD-CHAR
and also
GRE
What should be the perfect regex in my case?
Maybe you want to use \b word boundaries:
(\bGRE\b)
Here is the explanation
Demo: http://regex101.com/r/hJ3vL6
MD, if I understood your spec, this simple regex should work for you:
\W?GRE(?!\w)(?:\W\w+)?
But I would prefer something like [:-]?GRE(?!\w)(?:[:-]\w+)? if you are able to specify which non-word characters you are willing to allow (see explanation below).
This will match
GRE
GRE:Math
GRE-Math
but not CONGRES
Ideally though, I would like to replace the \W (non-word character) with a list of allowable characters, for instance [-:] Why? Because \W would match non-word characters you do not want, such as spaces and carriage returns. So what goes in that list is for you to decide.
How does this work?
\W? optionally matches one single non-word character as you specified. Then we match the literal GRE. Then the lookahead (?!\w) asserts that the next character cannot be a word character. Then, optionally, we match a non-word character (as per your spec) followed by any number of word characters.
Depending on where you see this appearing, you could add boundaries.
You can use the regex: /\bGRE\b/g
if(/\bGRE\b/g.test("string")){
// the string has GRE
}
else{
// it doesn't have GRE
}
\b means word boundary

Accented characters and regular expression

I have this regexp:
(\b)(emozioni|gioia|felicità)(\b)
In a string like the one below:
emozioni emozioniamo felicità felicitàs
it should match the first and the third word. Instead it matches the first and the last. I assume it is because of the accented character. I tried this alternative:
(\b)(emozioni|gioia|felicità\s)(\b)
but it matched "felicità" only if there is an other word after it. So for being specific only if it is in this context:
emozioni emozioniamo felicità felicitàs
and not in this other:
emozioni emozioniamo felicitàs felicità
I've found an article about accented characters in French (so at the beginning of the word) here, i followed the second answer. If anyone knows a better solution it is very welcome.
A word boundary \b works only with characters that are in \w character class, i.e [0-9a-zA-Z_], thus you can't put a \b after an accentued character like à.
You can solve the problem in your case using a lookahead:
felicità(?=\s|$)
or shorter:
felicità(?!\S)
(or \W in place of \s as suggested #Sniffer, but you take the risk to match something like :felicitàà)
Try the following alternative:
\b(emozioni|gioia|felicità)(?=\W|$)
This will match any of your listed words, as long as any of those words is followed by either a non-word character \W or end-of-string $.
Regex101 Demo

Trying to write a regex that matches only numbers,spaces,parentheses,+ and -

I'm trying to write a regular that will check for numbers, spaces, parentheses, + and -
this is what I have so far:
/\d|\s|\-|\)|\(|\+/g
but im getting this error: unmatched ) in regular expression
any suggestions will help.
Thanks
Use a character class:
/[\d\s()+-]/g
This matches a single character if it's a digit \d, whitespace \s, literal (, literal ), literal + or literal -. Putting - last in a character class is an easy way to make it a literal -; otherwise it may become a range definition metacharacter (e.g. [A-Z]).
Generally speaking, instead of matching one character at a time as alternates (e.g. a|e|i|o|u), it's much more readable to use a character class instead (e.g. [aeiou]). It's more concise, more readable, and it naturally groups the characters together, so you can do e.g. [aeiou]+ to match a sequence of vowels.
References
regular-expressions.info/Character Class
Caveat
Beginners sometimes mistake character class to match [a|e|i|o|u], or worse, [this|that]. This is wrong. A character class by itself matches one and exactly one character from the input.
Related questions
Regex: why doesn’t [01-12] range work as expected?
Here is an awesome Online Regular Expression Editor / Tester! Here is your [\d\s()+-] there.
/^[\d\s\(\)\-]+$/
This expression matches only digits, parentheses, white spaces, and minus signs.
example:
888-111-2222
888 111 2222
8881112222
(888)111-2222
...
You need to escape your parenthesis, because parenthesis are used as special syntax in regular expressions:
instead of '(':
\(
instead of ')':
\)
Also, this won't work with '+' for the same reason:
\+
Edit: you may want to use a character class instead of the 'or' notation with '|' because it is more readable:
[\s\d()+-]
Try this:
[\d\s-+()]

Categories