Javascript RegExp - it include space and brackets around - javascript

Bit of a noob to regexp
Please check out my attempt.
I want to isolate numbers that do not have hyphen or other characters around them apart from brackets - and then place quotes around these digits
so far I have - [^a-z-0-9](\d+)[^0-9-a-z]
match group of digits - that does not start or end with numbers or charachters
It is currently matching (1, 2) instead of say 1 and 2
Test
(0-hyphen-number) OR
(123 no hyphen) OR
(no hyphen 2) OR
(no 3 hyphen) OR
(no -4- hyphen) OR
(no -5 hyphen) OR
(no 6- hyphen) OR
(blah 0987 hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
Expected ouput :)
(0-hyphen-number) OR
("123" no hyphen) OR
(no hyphen "2") OR
(no "3" hyphen) OR
(no -4- hycphen) OR
(no -5 hyphden) OR
(no 6- hyphen) OR
(blah "0987" hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)

Your regex is close enough. You should however put - either at end or at beginning or character class.
You should capture all groups and replace them as follows.
Regex: ([^a-z0-9-])(\d+)([^0-9a-z-])
Replacement to do: Replace with $1"$2"$3
Regex101 Demo

do not have hyphen or other characters around them apart from brackets
You should take note that your original regex [^a-z-0-9](\d+)[^0-9-a-z]
matches any punctuation around the digits.
So, ,888+ and ,888] or *888} will match.
But what you're probably looking for is something like this
(?:^|[\s()])(\d+)(?:[\s()]|$)
which only allows whitespace boundary or parenth's boundary.
Change [\s()] to [\s(] or [\s}] to suite your needs.
Modification: To get possibly whitespace separated numbers as well.
https://regex101.com/r/pO4mO1/3
(?:^|[\s()])(\d+(?:\s*\d)*)(?:[\s()]|$)
Expanded
(?:
^
| [\s()]
)
( # (1 start)
\d+
(?: \s* \d )*
) # (1 end)
(?:
[\s()]
| $
)

By the time I loaded Regex101, it already had this working regex: [^a-z-0-9](\d+)[^0-9-a-z]
FYI (for everyone confused), in earlier revisions of the post, the regex was ^a-z-0-9[^0-9-a-z]. Another user edited the post to reflect what they saw in the demo.

Related

Match a Particular set of string with regex

I am trying to match a particular set of strings with a regex
1- #1 – .75 Gallon $16.99
2- #2 –1.6 Gallon $36.99
This is what I tried to figure out with many attempts but still it doesn't seems to work
console.log(/^#\d\s+–\s+[0-9]*\.[0-9]+\s+[a-zA-Z]+\s+:[0-9]*\.[0-9]+$/.test('#2 – 1.6 Gallon $36.99'))
console.log(/^#\d\s+–\s+[0-9]*\.[0-9]+\s+[a-zA-Z]+\s+:[0-9]*\.[0-9]+$/.test('#1 – .75 Gallon $16.99'))
I have gone through each part individually but I don't know where I am making mistake ,any help would be really appreciated.
Thanks
You should allow any (even zero) amount of whitespaces around the hyphen, and you need to match a dollar symbol instead of a colon:
^#\d\s*–\s*\d*\.?\d+\s+[a-zA-Z]+\s+\$\d*\.?\d+$
See the regex demo.
I also added a ? quantifier after \. to match integers.
Details:
^ - start of string
# - a # char
\d - a digit
\s*–\s* - a hyphen wrapped with zero or more whitespaces
\d*\.?\d+ - an integer or float like value: zero or more digits, an optional . and then one or more digits
\s+ - one or more whitespaces
[a-zA-Z]+ - one or more letters
\s+ - one or more whitespaces
\$ - a $ char
\d*\.?\d+ - an integer or float like value
$ - end of string.

Negative lookbehind in javascript

I am using the following regex:
https://(dev-|stag-|)(assets|images).server.io/v[\d]/file/(.*?)/(?!(download$))
Url 1: https://assets.server.io/v3/file/blt123e25b85f95497/download.jpg
Url 2: https://images.server.io/v3/file/blt123e25b85f95497/download
Url 3: https://images.server.io/v3/file/blt123e25b85f95497/random.jpg
The intention is to match Url 1 & 3 completely, but not Url 2, but it doesn't seem to work.
By checking the following answers:
Javascript regex negative look-behind,
Regex: match everything but,
I believe a negative lookbehind would work, but am unable to figure out what the regex for that would be.
Any help with it would be greatly appreciated!
The (?!(download$)) part by itself isn't doing the right thing here since it fails the match if there is download and end of string immediately to the right of the last / matched. You need to actually match the last subpart with a consuming pattern to actually match the filename.
You may use
/https:\/\/(dev-|stag-)?(assets|images)\.server\.io\/v\d\/file\/(.*?)\/(?!download$)[^\/]+$/
^^^^^^^^^^^^^
See the regex demo. If you need to match the whole string, add ^ anchor at the start of the pattern. s may be also made optional with ? after it.
Details
https:\/\/ - a https:// substring
(dev-|stag-)? - an optional dev- or stag- substring
(assets|images) - either assets or images substring
\.server\.io\/v - a .server.io/v substring
\d - any digit
\/file\/ - a /file/ substring
(.*?) - any 0+ chars other than line break chars, as few as possible
\/ - a /
(?!download$) - there must not be a download substring followed with the end of string position immediately to the right of the current location
[^\/]+ - 1 or more chars other than /, as many as possible
$ - end of string.
Note that [\d] is less readable than \d, and you need to escape . symbols in the pattern if you want to match literal dot chars.

JavaScript regular expression ignore case exception (Invalid group)

I have the following Regular expression :
(?i:(?:(?:(?:fbx|fo))\d+)|(?:(09|0[1-5])\s?(?:\d{2}\s?){4})(?:(#freeadsl)?))
I tested the expression in https://regex101.com/ and it works.
But in javascript, it dosen't work.
After doing a search, it turned out that the problem is that javascript doesn't accept regex ignore case ?i.
What's the best solution to remedy this problem.
Any help, i'll appreciate it, thanks !
JavaScript regex engine does not support inline modifier groups. You may use a i modifier in the JS regex and remove unnecessary non-capturing groups to reduce your regex to
var rx = /(?:fbx|fo)\d+|(?:09|0[1-5])\s?(?:\d{2}\s?){4}(?:#freeadsl)?/i;
^
See the regex demo. The /i at the end makes the letters in the pattern match both lower- and uppercase letters.
Details:
(?:fbx|fo)\d+ - fbx or fo substring followed with 1+ digits
| - or
(?:09|0[1-5]) - 09 substring or 0 followed with 1 to 5 digit.
\s? - an optional (1 or 0) whitespaces
(?:\d{2}\s?){4} - 4 occurrences of:
\d{2} - 2 digits
\s? - an optional (1 or 0) whitespaces
(?:#freeadsl)? - an optional #freeadsl substring.

Regex to match digits only if not followed or proceeded by letters in Javascript

I would like to match digits but not when they are within words (in JavaScript).
The following should match:
1
1,2
1.5-4 (matches 1.5 & 4 separately)
(1+3) (matches 1 & 3 separately)
=1;
The following should NOT match:
FF3D
3deg
I thought I could solve with with a negative lookahead, like so: (?![A-Za-z]+)([0-9]+[\.|\,]?[0-9]?) but it does not work.
How can I best solve this? Thanks.
I would like to match digits but not when they are within words.
You can use look arounds in your regex:
\b\d*[,.]?\d+\b
\b is for word boundary
RegEx Demo
2021 update:
Since lookbehind support has grown considerably, it makes sense to use a lookbehind based solution:
/(?<![a-z])\d*[.,]?\d+(?![a-z])/gi # ASCII only
/(?<!\p{L})\p{N}*[.,]?\p{N}+(?!\p{L})/giu # Unicode-aware
See the regex demo. Please track the lookbehind and Unicode property class support here.
Details
(?<![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the left of the current location
\d*[.,]?\d+
(?![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the right of the current location.
Original answer
In order to match any standalone integer or float numbers with dot or comma as decimal separator you need
/(?:\b\d+[,.]|\B[.,])?\d+\b/g
See the regex demo. The point here is that you cannot use a word boundary \b before a . since it will invalidate all matches like .55 (only 55 will be matched).
Details:
(?:\b\d+[,.]|\B[.,])? - either of the two alternatives:
\b\d+[,.] - a word boundary (there must be a non-word char before or start of string), then 1+ digits, and then a . or ,
| - or
\B[.,] - a position other than word boundary (only a non-word char or start of string) and then a . or ,
\d+ - 1+ digits
\b - a word boundary.
const regex = /(?:\b\d+[,.]|\B[.,])?\d+\b/g;
const str = `.455 and ,445 44,5345 435.54 4444
1
1,2
1.5-4
(1+3)
=1;
FF3D
3deg`;
console.log(str.match(regex));
If you need to also add support for the exponent use:
/(?:\b\d+[,.]|\B[.,])?\d+(?:e[-+]?\d+)?\b/ig
Try This
var pattern= /([\d.]+)/;
https://regex101.com/r/q1qDHV/1

JS regex to match a username with specific special characters and no consecutive spaces

I am pretty new to this reg ex world. Struck up with small task regarding Regex.
Before posting new question I have gone thru some answers which am able to understand but couldnt crack the solution for my problem
Appreciate your help on this.
My Scenario is:
Validating the Username base on below criteria
1- First character has to be a-zA-Z0-9_# (either of two special characters(_#) or alphanumeric)
2 - The rest can be any letters, any numbers and -#_ (either of three special characters and alphanumeric).
3 - BUT no consecutive spaces between words.
4- Max size should be 30 characters
my username might contain multiple words seperated by single space..for the first word only _# alphanumeric are allowed and for the second word onwards it can contain _-#aphanumeric
Need to ignore the Trailing spaces at the end of the username
Examples are: #test, _test, #test123, 123#, test_-#, test -test1, #test -_#test etc...
Appreciate your help on this..
Thanks
Arjun
Here you go:
^(?!.*[ ]{2,})[\w#][-#\w]{0,29}$
See it working on regex101.com.
Condition 3 is ambigouus though as you're not allowing spaces anyway. \w is a shortcut for [a-zA-Z_], (?!...) is called a neg. lookahead.
Broken down this says:
^ # start of string
(?!.*[ ]{2,}) # neg. lookahead, no consecutive spaces
[\w#] # condition 1
[-#\w]{0,29} # condition 2 and 4
$ # end of string
This might work ^(?=.{1,30}$)(?!.*[ ]{2})[a-zA-Z0-9_#]+(?:[ ][a-zA-Z0-9_#-]+)*$
Note - the check for no consecutive spaces (?! .* [ ]{2} ) is not really
necessary since the regex body only allows a single space between words.
It is left in for posterity, take it out if you want.
Explained
^ # BOS
(?= .{1,30} $ ) # Min 1 character, max 30
(?! .* [ ]{2} ) # No consecutive spaces (not really necessary here)
[a-zA-Z0-9_#]+ # First word only
(?: # Optional other words
[ ]
[a-zA-Z0-9_#-]+
)*
$ # EOS

Categories

Resources