How to make the below regex to accept any special character - javascript

/^[^ ]([\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.

Related

Regular expression to only allow letters and space in input filed but the first character should not be space

New to regex. I am validating a input filed. I need to write regular expression which checks the following:
Only letters and spaces are allowed not numbers and special charactes
The first character should be a valid letter [a-zA-Z] not a space.
Thank you
The following regex accepts only letters or spaces as you wanted.
^(?! )[A-Za-z\s]*$
Quick explanation:
^ Matches beginning of input
(?! ) Zero-width negative look-ahead assertion a.k.a. "not followed by...", so the first letter won't be a space character
[A-Za-z\s]* Accept only letters (A-Za-z) and spaces (\s) any time (*)
$ Matches end of input
Edit: If you don't want to match an empty string, then need to accept at least one character instead of any : ^(?! )[A-Za-z\s]+$

Regex disallow white Spaces at beginning and at end

i have this regex:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##\$%\^&\*])(?=.{8,26})
Password must obey the following rules:
cannot contain white spaces at the beginning or at the end.
must contain at least one number.
must contain at least 1 capital letter.
must contain at least 1 small letter.
must contain at least special character(!##$%^&*).
must be at least 8 characters min and max 26.
every thing works OK, but i cant disallow white spaces at beginning and at the end..
Any advise?
using javascript regex test..
You can use negative lookahead/lookbehind next to the begin/end string anchor points to accomplish this:
^(?!\s)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##$%^&*]).{8,26}(?<!\s)$
You can preserve your lookahead logic and add the couple of conditions you are missing like this:
(?=\S)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##\$%\^&\*])(?=.{8,26})(?=.*\S$).*
(?=\S) => Must start with a non whitespace char.
(?=.*\S$) => Must end with a non whitespace char.
You could add an extra positive lookahead (?=\S.{6,24}\S$) to assert that the string starts and ends with a non-whitespace character \S and update the quantifier to reflect the numbers of characters to {6,24}.
If your password could contain only the characters that you use in the lookaheads instead of matching all characters including whitespaces, you could use a character class again containing all the allowed characters.
Your regex could look like:
^(?=.*[A-Z])(?=.*[0-9])(?=.*[!##$%^&*])(?=\S.{6,24}\S$)[a-zA-Z0-9!##$%^&*]+$
See the regex demo

JavaScript regular expression for word boundaries, tolerating in-word hyphens and apostrophes

I'm looking for a Regular Expression for JavaScript that will identify word boundaries in English, while accepting hyphens and apostrophes that appear inside words, but excluding those that appear alone or at the beginning or end of a word.
For example, for the sentence ...
  She said - 'That'll be all, Two-Fry.'
... I want the characters shown in grey below to be detected:
  Shesaid- 'That'llbeall,Two-Fry.'
If I use the regex /[^A-Za-z'-]/g, then "loose" hyphens and apostrophes are not detected.
  Shesaid-'That'llbeall,Two-Fry.'
How can I alter my regex so that it detects apostrophes and hyphens that don't have a word character on both sides?
You can test my regex here: https://regex101.com/r/bR8sV1/2
Note: the text I will be working on may contain other writing scripts, like руский and ไทอ so it will not be feasible to simply include all the characters that are not part of any English word.
You can organize your word-boundary characters into two groups.
Characters that cannot be alone.
Characters that can be alone.
A regex that works with your example would be:
[\s.,'-]{2,}|[\s.]
Regex101 Demo
Now all that's left is to keep adding all non-word characters into those two groups until it fits all of your needs. So you might start adding symbols and more punctuation to those character classes.
You could write something like that:
(\s|[!-/]|[:-#]|[\[-`]|[\{-~])*\s(\s|[!-/]|[:-#]|[\[-`]|[\{-~])*
Or the compact version:
(\s|[!-/:-#\[-`\{-~])*\s(\s|[!-/:-#\[-`\{-~])*
The RegExp requires one \s (Space character) and selects als spaces and non alphanumeric chars before and after it.
https://regex101.com/r/bR8sV1/4
\s matches all spaces
!-/ every char from ! to /
:-# every char from : to #
\[-`` every char from [ to ``
\{-~ every char from { to ~

/(\S)\1(\1)+/g matching all occurrences of three equal non-whitespace characters following each other

Its given: /(\S)\1(\1)+/g matches all occurrences of three equal non-whitespace characters following each other.
I don't understand why there is () around (\S) and 2nd (\1), but not around 1st (\1). Can anyone help in explaining how above regex works?
src: http://www.javascriptkit.com/javatutors/redev2.shtml
Thnx in advance.
The \S needs parentheses to capture its value, so you can refer back to the captured value with \1. \1 means "match the same text which capturing group #1 matched".
I believe there is a problem with this regex. You said you want to match "three equal non-whitespace characters". But the + will make this match 3 or more equal, consecutive non-whitespace characters.
The g on the end means "apply this regex over the entire input string, or globally".
The second set of parentheses is not necessary. It needlessly captures the repeated character a second time, while matching the same strings as this regex:
/(\S)\1\1+/g
Also, as #AlexD pointed out, the description should say that it matches at least three characters. If you replaced that regex with BONK in the string fooxxxxxxbar:
'fooxxxxxxbar'.replace(/(\S)\1\1+/g, 'BONK')
..you might expect the result to be fooBONKBONKbar from their description, because there are two sets of three 'x's. But in fact the result would be fooBONKbar; the first \1 matches the second 'x', and the \1+ matches the third 'x' and any 'x's that follow it. If they wanted to match just three characters, they should have left the + off.
I noticed several other sloppy descriptions like that, plus at least one outright error: \B is equivalent to (?!\b) (a position that's not a word boundary), not [^\b] (a character that's not a backspace). For that matter, their description of word boundaries--"the position between a word and a space"--is wrong, too. A word boundary isn't defined by any particular character, like a space--in fact, it can just as well be the absence of any character that creates one. The string:
Word
...starts with a word boundary because 'W' is a word character and, being first, it's not preceded by another word character. Similarly, the 'd' is not followed by another word character, so the end of the string is also a word boundary.
Also, a regex doesn't know from words, only word characters. The definition of a word character can vary depending on the regex flavor and Unicode or locale settings, but it always includes [A-Za-z0-9_] (ASCII letters and digits plus the underscore). A word boundary is simply a position that's between one of those characters and any other character (or no other character, as I explained earlier).
If you want to learn about regexes, I suggest you forget that site and start here instead: regular-expressions.info.

What are the meanings of these regular expressions in JavaScript?

1) ^[^\s].{1,20}$
2) ^[-/##&$*\w\s]+$
3) ^([\w]{3})$
Are there any links for more information?
^[^\s].{1,20}$
Matches any non-white-space character followed by between 1 and 20 characters. [^\s] could be replaced with \S.
^[-/##&$*\w\s]+$
Matches 1 or more occurances of any of these characters: -/##&$*, plus any word character (A-Ba-b0-9_) plus any white-space character.
^([\w]{3})$
Matches three word characters (A-Ba-b0-9_). This regular expression forms a group (with (...)), which is quite pointless because the group will always equal the aggregate match. Note that the [...] is redundant -- might as well just use \w without wrapping it in a character class.
More info: "Regular Expression Basic Syntax Reference"
1) match everything without space what have 1 to 20 chars.
2) match all this signs -/##&$* plus words and spaces, at last one char must be
3) match three words
here is excelent source of regex
http://www.regular-expressions.info/
Matches any string that starts with a non-whitespace character that's followed by at least one and up to 20 other characters before the end of the string.
Matches any string that contains one or more "word" characters (letters etc), whitespace characters, or any of "-/##&$*"
Matches a string with exactly 3 "word" characters

Categories