Regex - printable ASCII that not contain some chars (JS) - javascript

I'm trying to find a Regex, that allows printable ASCII chars except - " / \ [ ] : ; | = , + * ? < >
The string length must be 1-25
This will work:
/^[^\\[\]\:\;\|\=\,\/+\*\?<>\"]{1,25}$/
But it will match also non-ASCII chars

You may use
/^(?:(?![:[\];|\\=,\/+*?<>"])[ -~]){1,25}$/
See the regex demo
Details
^ - start of string
(?: - outer grouping to enable matching a quantified lookahead-restricted ASCII char range
(?![:[\];|\\=,\/+*?<>"]) - the next char cannot be one defined in the character class (:, [, ], ;, |, \, =, ,, /, +, *, ?,
<, > and ")
[ -~] - any printable ASCII
){1,25} - 1 to 25 occurrences
$ - end of string

Related

Regex keep greedy even when using lazy quantifier

Here is my regex ^((\{.+:.+\})|([^{:}]))+?$.
Here is what i want:
Valid case: {test:test1} {test2:test3} test4 test5.
Invalid case: {test:}, {test1: test2, test1: test3}.
It's mean whenever my string have one of this three character: '{' ':', '}' it must also have 2 remaning character.
My regex is working well when my string not end with } character. I guess it's because of greedy quantifier. But i already put ? character after + quantifier it's still not working. What am I doing wrong?
You may use
^(?:\{[^{}:]*:[^:{}]*}|[^{:}])+$
See the regex demo.
Details
^ - start of string
(?:\{[^{}:]*:[^:{}]*}|[^{:}])+ - a non-capturing group matching 1 or more occurrences of
\{[^{}:]*:[^:{}]*} - a {, then any 0+ chars other than {, } and :, then :, then any 0+ chars other than {, } and :, and a }
| - or
[^{:}] - any char but {, } and :
$ - end of string.

Angularjs regex validation

I want to make an Angularjs regex validation that allow all alphabets, numbers, space and following special characters ! ' " & - ( )
You may use
ng-pattern="/^[a-zA-Z0-9\s!'\x22&()-]*$/"
Details
^ - start of string
[a-zA-Z0-9\s!'\x22&()-]* - 0 or more:
a-zA-Z - ASCII letters
0-9 - ASCII digits
\s - whitespaces
!'\x22&()- - !, ', " (\x22 matches "), &, (, ) or - (the hyphen must be at the end/start of the character class or escaped to denote a literal -)
$ - end of string.

regular expression to match name with only one spaces

I have a string condition in js where i have to check whether name entered in text box contains with one space only.
pattern: name.status === 'full_name' ? /^[a-zA-Z.+-.']+\s+[a-zA-Z.+-. ']+$/ : /^[a-zA-Z.+-. ']+$/
But the above regex matches names ending with 2 spaces also.
I need to match it such that the entered name should accept only one space for a name string. So the name will have only one space in between or at the end.
Two observations: 1) \s+ in your pattern matches 1 or more whitespaces, and 2) [+-.] matches 4 chars: +, ,, - and ., it is thus best to put the hyphen at the end of the character class.
You may use
/^[a-zA-Z.+'-]+(?:\s[a-zA-Z.+'-]+)*\s?$/
See the regex demo
Details
^ - start of string
[a-zA-Z.+'-]+ - 1 or more letters, ., +, ' or -
(?:\s[a-zA-Z.+'-]+)* - zero or more sequences of:
\s - a single whitespace
[a-zA-Z.+'-]+ - 1 or more letters, ., +, ' or - chars
\s? - an optional whitespace
$ - end of string.
Note: if the "names" cannot contain . and +, just remove these symbols from your character classes.
/^\S+\s\S+$/
try this
Some explanations:
^ - start of string
\s - single whitespace
\S - everything except whitespace
"+"- quantifier "one or more"
$ - end of string
you could also use word boundaries...
function isFullName(s) {
return /^\b\w+\b \b\w+\b$/.test(s);
}
['Giuseppe', 'Mandato', 'Giuseppe Mandato']
.forEach(item => console.log(`isFullName ${item} ? ${isFullName(item)}`))

remove unwanted groups of characters from string using regex

Given: 1999 some text here 1.3i [more]
Needed: some text here
The following regex - replace(/[\d{4} |\d\.*$]/,'') - failed, it just removed the first digit. Any idea why and how to fix it?
var s = "1999 some text here 1.3i [more]"
console.log(s.replace(/[\d{4} |\d\.*$]/,''))
The regex you have removes the first digit only because it matches just 1 char - either a digit, {, 4, }, space, |, ., * or $ (as [...] formed a character class), just once (there is no global modifier).
You may use
/^\d{4}\s+|\s*\d\..*$/g
See the regex demo
Basically, remove the [ and ] that form a character class, add g modifier to perform multiple replacements, and add .* (any char matching pattern) at the end.
Details:
First alternative:
- ^ - start of string
- \d{4} - 4 digits
- \s+ - 1+ whitespaces
Second alternative:
- \s* - 0+ whitespaces
- \d - a digit
- \. - a dot
- .* - any 0+ chars up to...
- $ - the end of the string
var rx = /^\d{4}\s+|\s*\d\..*$/g;
var str = "1999 some text here 1.3i [more]";
console.log(str.replace(rx, ''));

Assistance with Regex regarding limiting alphabetical characters

I am having some trouble setting up RegEx's. Could somebody help me in building a RegEx with these conditions:
Accepts only numbers (0-9)
Accepts a period (.), a negative sign (-), a plus sign (+), a dollar sign ($), and a comma (,)
Does not allow any alphabetical characters (a-z, A-Z)
Try the following expression
[0-9,.$\-\+]
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57)
,.$ matches a single character in the list ,.$
\- matches the character - literally
\+ matches the character + literally
This regex expression works for me:
^[0-9$.,+-]+$
Explanation:
^ matches the beginning of the string
[] A character set (matches characters listed)
0-9 Any number from 0-9
$ Matches a $ sign
- matches a - sign
. matches a .
, matches a ,
+ matches a + sign
+ One or more of the characters listed
$ End of the string
You can test this here

Categories

Resources