Check for special characters in string
I suggest using RegExp .test()
function to check for a pattern match, and the only thing you need to change is remove the start/end of line anchors (and the *
quantifier is also redundant) in the regex:
var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;// ^ ^ document.write(format.test("My@string-with(some%text)") + "<br/>");document.write(format.test("My string with spaces") + "<br/>");document.write(format.test("MyStringContainingNoSpecialChars"));
Check is string contains special characters and at least 2 characters amongs digit and letter
You can assert the 2 occurrences of a character or digit in the same lookahead, and then match at least a single "special" character.
Using a case insensitive pattern:
^(?=(?:[^a-z\d\n]*[a-z\d]){2})[a-z\d]*[~!@#$%^&*()_+<>•`{}\\][~!@#$%^&*()_+<>•`{}\\a-z\d]*$
The pattern matches:
^
Start of string(?:[^a-z\d\n]*[a-z\d]){2}
Assert 2 occurrences of either a char a-z or a digit. The[^a-z\d\n]*
part negates the character class using[^
to prevent unnecessary backtracking[a-z\d]*
Match optional chars a-z or a digit[~!@#$%^&*()_+<>•`{}\]
Match a special character[~!@#$%^&()_+<>•`{}\a-z\d]
Match optional allowed chars$
End of string
Regex demo
Check for special characters (/*-+_@&$#%) in a string?
The easiest way it to use a regular expression:
Regular Expression for alphanumeric and underscores
Using regular expressions in .net:
http://www.regular-expressions.info/dotnet.html
MSDN Regular Expression
Regex.IsMatch
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
if(regexItem.IsMatch(YOUR_STRING)){..}
Regex pattern to check if string has no special characters - Java
You missed a \
before the -
. You should use
^[a-zA-Z0-9_\-.]*$
See the demo
RegEx- first character should not contain special characters and subsequent characters should not contain few special characters
You were close. Just missing using ^
inside a character class to negate it, and maybe +
instead of *
to allow an empty string.
Match start of string: ^
Match a single character that's not one of !@#$%^&*()_+-=[]{};':"|,.<>/?: [^!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/\?]
Match a string of any length that doesn't contain one of !@$%^*+=[]{};:|<>?: [^!@$%^*+=\[\]{};:\\|<>\?]*
Match end of string: $
Complete regex: ^[^!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/\?][^!@$%^*+=\[\]{};:\\|<>\?]*$
Related Topics
JavaScript Math, Round to Two Decimal Places
What's the Purpose of Prototype
React Router V6 Navigate Outside of Components
Rails - Could Not Find a JavaScript Runtime
What Is Ajax and How Does It Work
Parallel Asynchronous Ajax Requests Using Jquery
How to Use React Hooks in React Classic 'Class' Component
How to Stop JavaScript Execution
Angularjs - Any Way for $Http.Post to Send Request Parameters Instead of JSON
Jquery (Or Pure Js) Simulate Enter Key Pressed for Testing
Jquery to Loop Through Elements with the Same Class
How to Check If Iframe Is Loaded or It Has a Content
Variable Scope in D3 JavaScript
Best Way to Get the Key of a Key/Value JavaScript Object