2

i would like to check string using js's regex. i would like write a pattern which is like this; if string contains [^a-zA-Z0-9_] return must be true else return must be false.

Thank you very for your help already now.

3 Answers 3

5

Use .test:

/[^a-zA-Z0-9_]/.test(str);

Edit:

/[^a-zA-Z0-9_İıĞğÇçÖöÜüÖö]/.test(str);

However, your regular expression will basically check whether there is a character that's not in that list in your string. I don't see the practical use of that. Is it indeed what you're trying to acoomplish?

e.g. a will fail, a- will not fail because the - does match the regexp.

Edit 2: If you want to make sure the string contains only certain characters, use this:

/^[a-zA-Z0-9_-İıĞğÇçÖöÜüÖö]*$/.test(str);

What it does is checking whether there are one or more of the characters of that list in the string, and ^...$ means that it only matches the complete string, so that a% or something like that does not pass (otherwise it would match the a and return true).

Edit 3: If a string should not contain any of a list of characters, use e.g.:

/^[^İıĞğÇçÖöÜüÖö]*$/.test(str); // returns true if there are not any of those characters in the string
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, One more question how to exclude İıĞğÇçÖöÜüÖö chars from a string?
Clearly, i just want to the string contains only english chars (uppercase or lowercase), numbers and specials chars like "_- ". My default languge is Turkish.
i would like to the string does not contains İıĞğÇçÖöÜüÖöŞş%& if string contains any chars of this array "İıĞğÇçÖöÜüÖöŞş" callback return me false with using js.
@Kerberos: Oh that is a different question. I thought you wanted to combine them.
Thank you very much for your help and sorry.
|
0
var string = 'Hello Sir';
var isMatch = /[^a-zA-Z0-9_]/.test(string);

Comments

0
return /\w/.test(string); // matches Letters,Numbers,and underscore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.