Regex for email (shouldn't match string after ; ) - javascript

I have the following regex: /(\s?[^\s,]+#[^\s,]+.[^\s,]+\s?;)*(\s?[^\s,]+#[^\s,]+.[^\s,]+)/g
Could you tell me why it matches this string: "hNw6B#90.com;tesr"
and doesn't match this one: "hNw6B#90.com; test" ?
It shouldn't match the first string. However if there is a valid email after the ; like test#abv.bg, it should be matched.
I will be very grateful if you could help me out.

You can use
^[^#\s;]+#[^\s;]+\.[^\s;]+(?:\s*;\s*[^#\s;]+#[^\s;]+\.[^\s;]+)*;?$
See the regex demo. Details:
^ - start of string
[^#\s;]+ - zero or more chars other than #, whitespace and ;
# - a # char
[^\s;]+ - zero or more chars other than whitespace and ;
\. - a dot
[^\s;]+ - zero or more chars other than whitespace and ;
(?:\s*;\s*[^#\s;]+#[^\s;]+\.[^\s;]+)* - zero or more repetition of a ; enclosed with zero or more whitespaces, and then the same pattern as above
;? - an optional ;
$ - end of string.

Related

Regex to not allow special character without prefix or suffix

I was writing regex for the following validate a string. I wrote the following regex.
^[^\s]+[a-z]{0,}(?!.* {2})[ a-zA-z]{0,}$
it validates for
No space in beginning.
no two consecutive space allowed.
The problem is it allows a single special character. it should not allow a special character unless it is suffixed or prefixed with alpha-numeric character.
Examples:
# -> not allowed.
#A or A# or A2 or 3A is allowed.
One option is to assert that the string does not contain a single "special" char or 2 special chars next to each other using a negative lookahead.
^(?!.*[^a-zA-Z0-9\s][^a-zA-Z0-9\s])(?!.*(?:^| )[^a-zA-Z0-9\s](?!\S))\S+(?: \S+)*$
Explanation
^ Start of string
(?! Negative lookahead, assert that what is at the right does not contain
.*[^a-zA-Z0-9\s][^a-zA-Z0-9\s] match 2 chars other than a-zA-Z0-9 or a whitespace char next to each other
) Close lookahead
(?! Negative lookahead, assert that what is at the right does not contain
.*(?:^| )[^a-zA-Z0-9\s](?!\S) Match a single char other than a-zA-Z0-9 or a whitespace char
) Close lookahead
\S+(?: \S+)* Match 1+ non whitespace chars and optionally repeat a space and 1+ non whitespace chars
$ End of string
Regex demo
Please omit the '$' symbol from the regex because it represents the end of the sentence.
^[^\s]+[a-z]{0,}(?!.* {2})[ a-zA-z]{0,}
So when applying the above regex to the following, it finds only '# '.
#A A# A2 3A

regex to allow special characters but not leading/trailing white space

I would like to allow all special characters and white space in between words only for a password input field.
If whitespace entered at the leading, trailing of string, regex should fail
Any useful javascript regex?
I tried \S this does not accept any white space, would that be sufficient?
I tried \A\s|\s*\Z , but not able to negate this.
Using something like [^\s] would suffice.
The \A (start of string) and \Z (end of string) anchors are not supported by JS RegExp.
If you use /\S/ it will only match any non-whitespace char, anywhere inside a string.
If you use /^\s|\s*$/ it will match a whitespace at the start or any 0 or more whitespaces at the end.
You need
/^\S+(?:\s+\S+)*$/
See the regex demo.
It will match:
^ - start of string
\S+ - 1 or more non-whitespace chars
(?:\s+\S+)* - any 0 or more occurrences of
\s+ - 1+ whitespaces
\S+ - 1+ non-whitespace chars
$ - end of string.
JS demo:
var strs = ['Abc 123 !##', 'abc123#', ' abc34', ' a ', 'bvc '];
var rx = /^\S+(?:\s+\S+)*$/;
for (var s of strs) {
console.log("'"+s+"'", "=>", rx.test(s));
}
I don't know if it's totally fine but in your case, I think this could apply better
^((\w)*){1}$

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)}`))

Regex - I want my string to end with 2 special character

I've been trying to make a regex that ends with 2 special characters, but I couldnt find solution. Here is what i tried, but it seems like it is not working.
/.[!##$%^&*]{2}+$/;
Thanks in advance.
Try this regex:
^.*[!##$%^&*]{2}$
Demo
const regex = /^.*[!##$%^&*]{2}$/;
const str = `abc##\$`;
let m;
if(str.match(regex)) {
console.log("matched");
}
else
console.log("not matched");
The /.[!##$%^&*]{2}+$/ regex matches
. - any character but a line break char
[!##$%^&*]{2}+ - in PCRE/Boost/Java/Oniguruma and other regex engines supporting possessive quantifiers, it matches exactly 2 cars from the defined set, but in JS, it causes a "Nothing to repeat" error
$ - end of string.
To match any string ending with 2 occurrences of the chars from your defined set, you need to remove the . and + and use
console.log(/[!##$%^&*]{2}$/.test("##"))
Or, if these 2 chars cannot be preceded by a 3rd one:
console.log(/(?:^|[^!##$%^&*])[!##$%^&*]{2}$/.test("##"))
// ^^^^^^^^^^^^^^^^^
The (?:^|[^!##$%^&*]) non-capturing group matches start of string (^) or (|) any char other than !, #, #, $, %, ^, &, * ([^!##$%^&*])

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, ''));

Categories

Resources