Simple Regex skips always first character on a line [duplicate] - javascript

This question already has answers here:
Regex that can match empty string is breaking the javascript regex engine
(2 answers)
Closed 4 years ago.
I have a very strange effect when using a particular regex in JavaScript. If I use /^|.+/gm, it always skips the first character on a line.
According to regex101.com, it doesn't happen with pcre (php), but does happen in JavaScript, Python, and GoLang. Any ideas on why this could be happening?

In Javascript, empty matches still increment the current index being searched in the string by one. If anything is matched starting at position X in the string, the next match must start at least at position X + 1. (PCRE does not exhibit this behavior; empty matches which don't consume any characters permit an additional non-empty match immediately following that empty match)

Related

No white space at beginning + allow space in the middle [duplicate]

This question already has answers here:
Regular expression: match start or whitespace
(8 answers)
Closed 1 year ago.
I have this regex for detecting #xxx
/(?:#)(.*[a-zA-Z0-9]*)/
it matches even when the #xxx is not separated from another string from the left (when it's typed in the middle of an input line).
xxx#xxx will match too so i added \s to require a space in the begining .Now it's
/\s(?:#)(.*[a-zA-Z0-9]*)/
But the problem is there isn't a match when the #xxx is typed in the begining of a line (the white space is still required) and i need it match in that case.
I tried to get inspired by https://stackoverflow.com/a/19973707/170592 so i added ^[^-\s] in the begining of the regex to make it
/^[^-\s](?:#)(.*[a-zA-Z0-9]*)/
But it didn't work neither.
I think that what you are looking for it is /\S+/ which means that check for any non-whitespace and I don't think you need the ^ at the beginning.
[-\S+](?:#)(.*[a-zA-Z0-9]*)

Matching the first line of content in RegEx [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 8 months ago.
I want to match numbers between 0 and 799 ONLY,if it doesn't have a comma in them.
$660
http://stackoverflow.com/questions/6560030/what-regex-can-i-use-tovalidate-anumber-between-0-and-255
I've tried using this RegEx. --> \b(0*(?:[0-9]?[0-9]?[0-9]?|100))\b and it works very well.
(If the number is between 0 and 999)
Need help with changing my regex:
I need it to work in JavaScript.
I'd like to validate the number in the first row using regex after the $ (I only need
It,if It's between 0 and 799)
If it has a comma in it then it should be ignored( like numbers 799+)
I don't want it to accept numbers with comma in them,because my current regex thinks it's valid.
(Or at least the 6,245 should be equal to 6245 so my regex can ignore it.)
To rephrase your question, numbers must not have a comma before them, or after:
(?<!,)\b[1-7]?\d?\d\b(?!,)
Try it online.
If you can't use look behinds, eg if you use JavaScript, you'll have to consume the non-comma and capture the target instead:
(^|[^,])(\b[1-7]?\d?\d\b(?!,))
The number is in group 1.
Try it online.

Negative look-ahead always returns true [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 7 years ago.
I am using this regular expression
/(?!results)/i
As far as I understand, it will match any string that does not contain the word "result". However, when I try
/(?!results)/i.test('basketball results')
it returns true. How do I match strings that do not contain the word results?
This regex matches every position that has no results after it. See demo.
To match an expression that does not contain results, you need to use ^(?!.*results.*$).*. See another demo.
You can use a simple indexOf check here. It will return -1 is a substring is not contained in the string and zero or greater otherwise:
"basketball results".indexOf('results') == -1

Regex pattern to match this string [duplicate]

This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group

RegExp - How can I match the shortest amount possible? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I have my Regular Expression /'(.*)(?:(?:'\s*,\s*)|(?:'\)))/
and my test code ('He said, "You're cool."' , 'Rawr')
(My test code simulates parameters being passed into a function.)
I will explain my Regular Expression as I understand it and hopefully a few of you can shed some light on my problem.
1)/' means at the beginning of the matched string, there needs to be '
2)(.*) means capture any character except \n 0 or more times
3)(?:(?:4)|(?:5)) means don't capture but try to do step 4 and if it doesn't work try step 5
4)(?:'\s*,\s*) means don't capture but there needs to be a ' with 0 or more whitespace characters followed by a , with 0 or more whitespace characters
5)(?:'\)) means don't capture but there needs to be ')
So it seems that it should return this (and this is what I want):
'+He said, "You're cool."+' ,
But it returns:
'+He said, "You're cool."' , 'Rawr+')
If I change my test code to ('He said, "You're cool."' , 'Rawr' (no end parenthesis) it returns what I want, but as soon as I add that last parenthesis, then it seems that my OR operator does whatever it wants to. I want it to test first if there is a comma, and break there if there is one, and if there is not one check for a parenthesis.
I've tried switching the spots of step 4 and step 5, but still the OR operator seems to always default to the (?:'\)) side.
How can I match the shortest amount possible?
I don't think your problem is the OR operator, but the greediness of the .*. It will match your full string, and then back-track until the following expressions match. The first match in this backtracking process will be 'He said, "You're cool."' , 'Rawr+'). Try .*? instead!

Categories

Resources