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, ''));
Related
Is it possible to merge these two regular expressions?
/^[A-Za-z]\S{3,30}$/
/^(?:(\w)(?!\1\1))+$/
I would like a string of:
only letters,
length between 3 and 30,
no spaces,
no to the repetition of the same letter more than two consecutive times (e.g: 'ddd' //false, 'dtdddyyt' //false, 'dtddyyt'//true).
You can use the second pattern as a negative lookahead assertion once to not match ddd in the string.
^(?!\S*(\w)\1\1)[A-Za-z]\S{3,30}$
^ Start of string
(?! Negative lookahead
\S*(\w)\1\1 Match optional non whitespace chars, capture a word char and match the same with 2 backreferences
) Close lookahead
[A-Za-z]\S{3,30} Match a single char A-Za-z and 3-30 non whitespace chars
$ End of string
Regex demo
const regex = /^(?!\S*(\w)\1\1)[A-Za-z]\S{3,30}$/;
[
"ddad",
"dtddyyt",
"adaddd",
"dtdddyyt",
"ddd"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));
You can use
/^(?:([A-Za-z])(?!\1{2})){3,30}$/
/^(?:(\p{Alphabetic})(?!\1{2})){3,30}$/u
See the regex demo. Note:
only letters - [A-Za-z] / \p{L} or \p{Alphabetic} (available in ECMAScript 2018+ compliant JS environments with /u flag)
length between 3 and 30 - {3,30}
no spaces - this condition is already covered by the first one
no to the repetition of the same letter more than two consecutive times - ([A-Za-z])(?!\1{2}).
JavaScript test:
const rx = /^(?:([A-Za-z])(?!\1{2})){3,30}$/;
const texts = ["ddad","dtddyyt","adaddd","dtdddyyt","ddd"];
for (let text of texts) {
console.log(text, "=>", rx.test(text));
}
More details:
^ - start of string
(?: - start of a non-capturing group (used as a container of a pattern sequence):
([A-Za-z]) - Group 1: an ASCII letter (\p{L} / \p{Alphabetic} matches any Unicode letter)
(?!\1{2}) - right after the letter, there should not be two occurrences of the same letter
) - end of the group
{3,30} - match three to thirty consecutive occurrences of the pattern sequence inside the non-capturing group
$ - end of string.
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}$
I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string
I'm trying to write a regex to match an optional parameter at the end of a path.
I want to cover the first 4 paths but not the last one:
/main/sections/create-new
/main/sections/delete
/main/sections/
/main/sections
/main/sectionsextra
So far I've created this:
/\/main\/sections(\/)([a-zA-z]{1}[a-zA-z\-]{0,48}[a-zA-z]{1})?/g
This only finds the first 3. How can I make it match the first 4 cases?
You may match the string in question up the optional string starting with / with any 1 or or more chars other than / after it up to the end of the string:
\/main\/sections(?:\/[^\/]*)?$
^^^^^^^^^^^^^^
See the regex demo. If you really need to constrain the optional subpart to only consist of just letters and - with the - not allowed at the start/end (with length of 2+ chars), use
/\/main\/sections(?:\/[a-z][a-z-]{0,48}[a-z])?$/i
Or, to also allow 1 char subpart:
/\/main\/sections(?:\/[a-z](?:[a-z-]{0,48}[a-z])?)?$/i
Details
\/main\/sections - a literal substring /main/sections
(?:\/[^\/]*)? - an optional non-capturing group matching 1 or 0 occurrences of:
\/ - a / char
[^\/]* - a negated character class matching any 0+ chars other than /
$ - end of string.
JS demo:
var strs = ['/main/sections/create-new','/main/sections/delete','/main/sections/','/main/sections','/main/sectionsextra'];
var rx = /\/main\/sections(?:\/[^\/]*)?$/;
for (var s of strs) {
console.log(s, "=>", rx.test(s));
}
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)}`))