Regex working in every online parser but not in JS? [duplicate] - javascript

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 5 years ago.
I've created regex to parse football match score:
(0|[1-9]\d*\s)(:)(\s0|[1-9]\d*)
When I try:
let scoreRegex = new RegExp('(0|[1-9]\d*\s)(:)(\s0|[1-9]\d*)')
scoreRegex.exec('Team One 5 : 0 Team Two')
I get null instead of expected 3 groups. Any online parser out there validates my regex and matches the input string.
https://www.regextester.com/ +
https://regexr.com/ +
What am I missing?

Related

RegExp with variable in a string format [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.

JS Regex to extract all URL from a Text to array of URL [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expression to match string starting with a specific word
(10 answers)
Regular expression - starting and ending with a character string
(3 answers)
Closed 3 years ago.
I am trying to pull out all the URLs from a text entered by the user by doing the following but am not able to get the desired result.
let regexp = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$/igm;
let str = "https://mysleepyhead.com http://skreem.io";
let array = [...str.matchAll(regexp)];
console.log(array);
Desired output would be
Array ['https://mysleepyhead.com', 'http://skreem.io']

Javascript get string occurrences [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Closed 4 years ago.
I'm trying to return how many times the string s.t() was found in a string, but I can't get the correct regex for this...
For example
var string = 'function(test) {s.t(); s.t(dsabf); s.t();}'
var re = new RegExp('s\.t\(\)', "g");
return re;
should return an array of 2 elements ['s.t()', 's.t()'] but instead it has 3 elements ['s.t', 's.t', 's.t']
I've also tried with ^s\t\(\)$ but this returns no match...
How can I fix my regex in order to make this work as expected?

regex to match any 1+ non-blankspace character [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Match exact string
(3 answers)
Closed 4 years ago.
I need to have a Regex for matching a single (or greater) non- blank space character (would allow all special characters such as !,' etc...). Would
var filter = /\S+/;
be sufficient? This seems to work for 1 or greater.
Would:
var filter = /\S+/{3,};
be sufficient for 3 or more of the non-same characters (like "def", "a!c", "dA!!f", but not "some bird"?

getting a number using regular expressions [duplicate]

This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333

Categories