I'm trying this regular expressión, but I can't validate correctly the end white space and the letter:
/^\d{0,2}(\-\d{0,2})?(\-\d{0,2})?(\ ?\d[W,E]?)?$/
Examples of correct values:
33-39-10 N //OK
85-50 W //OK
-85-50 E //Wrong
What's wrong?
\d{0,2} this quantifier also matches a digit zero times so that would match the leading - in the 3rd example.
In the character class [W,E] you could omit the comma and list the characters you allow to match [ENW]
If only the third group is optional you could try including the whitespace before the end of the line $
^\d{2}(-\d{2})(-\d{2})? [ENW] $
I have used this regular expression : ^(?!\-)\d{0,2}?(\-\d{0,2}).+\s(N|E|W|S)$
Using a negative lookahead, we have excluded anything that starts with a dash (-).
(?!\-) = Starting at the current position in the expression,
ensures that the given pattern will not match
\s(N|E|W|S) matches anything with a space (\s) and one of the letters using OR operator |.
You may also use \s+(N|E|W|S).
+ = Matches between one and unlimited times, as many times as
possible, giving back as needed
Related
Currently, on the project, they have a pattern [^0-9,] and it replaces (String.prototype.replace) everything that we don't need. Except it's not so great. We can add a comma to the start and to the end of the string.
What do I need and I can't do it no matter how hard I try)
first should be a number in the range 0-9
after the first number should be a comma (one comma) or a number or numbers (0-9)
at the end of the line shouldn't be a comma
Correct example,
1,2,3
Incorrect,
,,,,1,,2dgd,d,
1,2,3,
,1,2,,,3
UPD: The method String.prototype.replace() is used on the project.
I'll be grateful if you help me!
If the comma's are optional, maybe you need:
^\d+(?:,\d+)*$
See the online demo
^ - Start string ancor
\d+ - One or more digits.
(?:- Open non-capture group.
,\d+ - A comma followed but one or more digits.
)* - Close non-capture group and match zero or more times.
$ - End string ancor.
Edit:
If you actually want to clean a string, maybe you could use:
(\d+).*?(,(?=.*\d))|\D
See the online demo. Just make sure to replace by $1$2.
(\d+) - 1st Capture group with one or more digits.
.*? - Lazy match anything opto:
(,(?=.*\d)) - 2nd Capture group to match literal comma with a nested positive lookahead to ensure there is still a digit ahead.
| - Or:
\D - Anything other than digit.
const regexp = new RegExp(/(\d+).*?(,(?=.*\d))|\D/g);
const value = '1,2,3'.replace(regexp, '$1$2');
console.log(value)
This should work:
^[0-9]+(?:,[0-9]+)*$
Begins with a digit or a set of digits, followed by zero or more occurrences of a comma followed by one or more digits. Don't miss the start and end line anchors.
Demo
I'm trying to get all capital letters between the last 2 parentheses in a string. So far I've tried this:
/\(([A-Z])([^)]*)\)[^(]*$/g
On for example: I don't want (These Words), I want (These Two)
but it gives me:
Group 1. T
Group 2. hese Two
Can someone help me?
Thanks in advance!
I think, the shortest and simplest solution is:
(?!.*\() - Negative lookahead - nowhere later can occur any
opening parenthesis (after any number of other chars),
(?=.*\)) - Positive lookahead - somewhere later there must
occur the closing parenthesis (after any number of other chars),
[A-Z] - Catch a capital letter, not as a capturing group,
but as a "normal" match,
g - With global option.
To sum up:
/(?!.*\()(?=.*\))[A-Z]/g
Code
var s = "I don't want (These Words), I want (These Two)"
var r = /.*\(([^)]*)\)/
var m = r.exec(s)
console.log(m[1].match(/[A-Z]/g))
Explanation
First Regex
The first regex .*\(([^)]*)\).* extracts the contents of the last parentheses.
.* Match any character any number of times
\( Match a left parenthesis literally
([^)]*) Capture any character except the right parenthesis any number of times into capture group 1
\) Match a right parenthesis literally
.* Match any character any number of times
Second Regex
The second regex [A-Z] matches uppercase letters
You can get all the capital letters between the last string wrapped between parentheses in JavaScript as follow:
/(?!.*\()(?=.*\))([A-Z])/g
Where the first part (?!.*\(.*\)) is a negative lookahead which ignores all the opening parentheses and stops immediately after the last one. Then we perform a positive lookahead which matches everything until it encounters closing parentheses. Then we match the capital letters.
I am trying to write a javascript regular expression that matches a min and max number of words based on finding this pattern: any number of characters followed by a space. This matches one word followed by an empty space (for example: one ):
(^[a-zA-Z]+\s$)
Debuggex Demo
When I add in the range quantifier {1,3}, it doesn't match two occurrences of the pattern (for example: one two ). What do I need to change to the regular expression to match a min and max of this pattern?
(^[a-zA-Z]+\s$){1,3}
Debuggex Demo
Any explanation is greatly appreciated.
Take ^ and $ out of the quantified group, because you can't match the beginning and end of the string multiple times in one line.
^([a-zA-Z]+\s){1,3}$
DEMO
The following will work exactly as specified:
^([a-zA-Z]+ ){1,3}$
Replace the space with \s to match any single whitespace character:
^([a-zA-Z]+\s){1,3}$
Add a quantifier to the \s to set how many whitespace characters are acceptable. The following allows one or more by adding +:
^([a-zA-Z]+\s+){1,3}$
If the whitespace at the end is optional, then the following will work:
^([a-zA-Z]+(\s[a-zA-Z]+){0,2})\s*$
(^[a-zA-Z]+\s$) will start scanning from the start of the line ^, scan for a word [a-zA-Z]+, scan for a space \s, and expect the end of the line $
When you have two words, it does not find the end of the line, so it fails. If you take out $, the second word would fail because it is not the start of the line.
So the start line and end line have to go around the limit scan.
To make it more generic:
(\S+\s*){1,3}
\S+: At least one Non-whitespace
\s*: Any amount of Whitespace
This will allow scanning of words even if there is no space at the end of the string. If you want to force the whole line, then you can put ^ in the front and $ at the end:
^(\S+\s*){1,3}$
I am trying to make regexp for validating string not containing
^ ; , & . < > | and having 1-20 characters. Any other Unicode characters are valid (asian letters for example).
How to do it?
You can use the following:
^[^^;,&.<>|]{1,20}$
Explanation:
^ assert starting of the string
[^ start of negated character class ([^ ])
^;,&.<>| all the characters you dont want to match
] close the negates character class
{1,20} range of matches
$ assert ending of the string
It will match any character other than specified characters within range of 1-20.
Your regex \w[^;,&.<>|]{1,20} contains \w that might not match all Unicode letters (I guess your regex flavor does not match Unicode letters with \w). Anyway, the \w only matches 1 character in your pattern.
Also, you say you need to exclude ^ but it is missing in your pattern.
When you want to validate length, you also must use ^/$ anchors to mark the beginning and end of a string.
To create a pattern for some range that does not match specific characters, you need a negated character class with anchors around it, and the length is set with limiting quantifiers:
^[^^;,&.<>|]{1,20}$
Or (this version makes sure we only match at the beginning and end of the string, never a line):
\A[^^;,&.<>|]{1,20}\z
Note that inside a character class, almost all special characters do not require escaping (only some of them, none in your case). Even the ^ caret symbol.
See demo
I would like to test if user type only alphanumeric value or one "-".
hello-world -> Match
hello-first-world -> match
this-is-my-super-world -> match
hello--world -> NO MATCH
hello-world-------this-is -> NO MATCH
-hello-world -> NO MATCH (leading dash)
hello-world- -> NO MATCH (trailing dash)
Here is what I have so far, but I dont know how to implement the "-" sign to test it if it is only once without repeating.
var regExp = /^[A-Za-z0-9-]+$/;
Try this:
/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/
This will only match sequences of one or more sequences of alphanumeric characters separated by a single -. If you do not want to allow single words (e.g. just hello), replace the * multiplier with + to allow only one or more repetitions of the last group.
Here you go (this works).
var regExp = /^[A-Za-z0-9]+([-]{1}[A-Za-z0-9]+)+$/;
letters and numbers greedy, single dash, repeat this combination, end with letters and numbers.
(^-)|-{2,}|[^a-zA-Z-]|(-$) looks for invalid characters, so zero matches to that pattern would satisfy your requirement.
I'm not entirely sure if this works because I haven't done regex in awhile, but it sounds like you need the following:
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)+$/
You're requirement is split up in the following:
One or more alphanumeric characters to start (that way you ALWAYS have an alphanumeric starting.
The second half entails a "-" followed by one or more alphanumeric characters (but this is optional, so the entire thing is required 0 or more times). That way you'll have 0 or more instances of the dash followed by 1+ alphanumeric.
I'm just not sure if I did the regex properly to follow that format.
The expression can be simplified to: /^[^\W_]+(?:-[^\W_]+)+$/
Explanation:
^ match the start of string
[^\W_]+ match one or more word(a-zA-Z0-9) chars
(?:-[^\W_]+)+ match one or more group of '-' follwed by word chars
$ match the end of string
Test: https://regex101.com/r/MODQxw/1