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
Related
The sample i wanna past through the regex is with follow requirement
list of number seperate by comma
at least input 10 number in list
only accept numbers but could allow empty space in both front and back
Here is my sample regex code:
^(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)+(?:,(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)){9,}$
This regex test code could pass list number seperate by comma,
however when i add empty space in front and on the back it will not work
The following testing Code is failed
' 2,2.5,.5, .678 ,39,1.4.4.8,2.4,2.5,2.6,2.7'
You might use
^\s*[+-]?\d*\.?\d+(?:\.\d+)*(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,}\s*$
The pattern matches:
^ Start of string
\s*[+-]?\d*\.?\d+ Match an optional plus or minus sign, optional digits and optional dot followed by 1 or more digits
(?:\.\d+)* As there are digits with multiple dot parts, you can optionally repeat that
(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,} That first part of the pattern matches 1 time. As you want to match at least 10 times you can repeat the first pattern 9 or more times, starting with a comma between optional whitespace chars
\s*$ Optional trailing whitespace chars and assert end of string
Regex demo
Note that \s can also match a newline.
If you don't want that, you could use for example a mere space or [ \t]
I'm trying to make a regular expression that only accepts:
Min 100 and atleast 1000 characters, characters “,’,<,> aren't allowed, two full stops one after another aren't allowed.
This is what I have for now:
^.{100,1000}$ → for 100 to 1000 characters
^[^"'<>]*$ → for the characters that aren't allowed
^([^._]|[.](?=[^.]|$)|_(?=[^_]|$))*$ → doesn't allow 2 consecutive dots
How do I combine this regex into one? ._.
This part [^._] means no dot or underscore and this part [.](?=[^.]|$)|_(?=[^_]|$) matches either a . or _ followed by the opposite or end of string.
You could write the pattern using a single negative lookahead assertion excluding __ or ..
^(?!.*([._])\1)[^"'<>\n]{100,1000}$
Explanation
^ Start of string
(?! Negative lookahead, assert that what is at the right is not
.*([._])\1 capture either . or _ and match the same captured char after it (meaning no occurrence of .. or __)
) Close lookahead
[^"'<>\n]{100,1000} Match 100-1000 times any character except the listed
$ End of string
Regex demo (with the quantifier set to {10,100} for the demo)
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
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 have a regular expression that I use to test against user input that expects currency. This statement allows an optional dollar sign, allows optional commas (as long as they are correctly placed), and allows a single decimal point as long as it's followed by at least another number.
^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$
Examples like
$12.12
0.34
12,000
12,000000
are all allowed by design. There is one however that doesn't match that I would like to. If a user wants to enter a number like .34 it must be proceeded by a zero. So 0.34 matches, but .34 doesn't.
Here's how I updated the statement to fix this.
^(\$?\d{1,3}(,?\d{3})*)?(\.\d{1,2})?$
I've made the entire statement before the decimal point a capturing group and made it optional. What I'm worried about now though, is that my entire regex statement enclosed by two capturing groups which are optional. I don't want a blank space to match this pattern and I think it will. Is there a better option for what I'm trying to accomplish?
Edit: My original statement doesn't match .12 The second updated statement does however, because the entire statement is wrapped in optional capturing groups, a blank space would match this pattern and that is not desired.
Your optional group is the correct way to proceed. Note that non-capturing groups that are only used to group sequences of subpatterns are more efficient when you do not have to access the captured subvalues later.
The only thing you really miss is to avoid matching an empty string. You may achieve it using a positive lookahead (?=.) or (?!$) negative lookahead:
^(?!$)(?:\$?\d{1,3}(?:,?\d{3})*)?(?:\.\d{1,2})?$
See the regex demo
Details
^ - start of string
(?!$) - no end of string right after the start of string
(?: - start of an optional non-capturing group
\$? - 1 or 0 $ symbols
\d{1,3} - 1 to 3 digits
(?:- start of a non-capturing group repeated 0+ sequences of
,? - 1 or 0 commas
\d{3} - 3 digits
)* - end of the non-capturing group
)? - end of the optional non-capturing group
(?:\.\d{1,2})? - an optional non-capturing group matching 1 or 0 sequences of
\. - a dot
\d{1,2} - 1 or 2 digits
$ - end of string.
You can use the "or" syntax |
^\$?(\d{1,3}(,?\d{3})*|0)(\.\d{1,2})?$
I would also suggest that you don't need to capture the inner groups of (,\d{3})*
^\$?(\d{1,3}(?:,?\d{3})*|0)(\.\d{1,2})?$
(\d{1,3}(?:,?\d{3})*|0) either an amount \d{1,3}(?:,?\d{3})* or 0