This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
This isn't so much about how to achieve what is explained in the example, there are plenty of easy ways to do it. The question is WHY does the example not behave as expected?
1. If I have the following string
"#foo#bar"
2. And I'm trying to get [foo, bar] with the following regex in JavaScript
"#foo#bar".match(/(?:#)([a-zA-Z]*)/gi)
3. It returns
Array [ "#foo", "#bar" ]
Ignoring the non-capturing group (?:#)
It works as I first expected in here https://regex101.com/r/zI0wH9/1
So why is this? Do JS regexes behave somehow differently?
simply try
"#foo#bar".match(/[a-zA-Z]+/gi); //outputs ["foo", "bar"]
Related
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 5 years ago.
Is there any way to make something.+()[]* matching literally 'something.+()[]*'? I'm using regex builder so manual escaping is not allowed. Sure, i can add hardcoded checks if (char === '+') return '\+' but i'm looking for native solution or better way
UPD
I'm sorry. I forgot to add that matching should be in given order with moving forward but not back. So [+.] will not fit my requirements because it will match both +. and .+. I need only first case (In definition order)
You don't need to escape them if within square brackets.. I just tested and works for me, but maybe not what you are looking for?
something[.+()[]]
This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 7 years ago.
I have seen several similar questions on SO, but not exactly what I'm looking for.
I want to use a variable in my regEx so that when I call it, I can easily pass in a number.
Here's the hard coded regEx:
'mywonderfullString'.match(/.{1,3}/g)
Here's what I need:
'mywonderfullString'.match(/.{1,variableHERE}/g)
So when I call the regEx, I would do something like
'mywonderfullString'.match(/.{1,3}/g)
I've seen some examples using the replace regEx, but I can't seem to my example working.
You need to use RegExp constructor in-order to include variables inside regex.
var variableHERE = '3'
alert('mywonderfullString'.match(new RegExp(".{1," + variableHERE + "}", "g")))
This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to convert following expression to "new Regexp()" style:
http://jsfiddle.net/HDWBZ/
var Wyrazenie = /\btest[a-z]*/g;
I'm really confused with it and have no idea how to fix it. Below is what I've done but obviously it doesn't work.
var Wyraznie = new RegExp("\btest[a-z]*","g");
Also have a question how would it look if instead of "test" I would use variable?
You should use this instead...
new RegExp("\\btest[a-z]*", "g");
... as \b will be interpolated into a single (slashless) character when JavaScript parser works through the corresponding string literal. The solution is to escape slash itself.
DEMO: http://jsfiddle.net/HDWBZ/1/
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I'm trying to use RegEx to select all strings between two dollar signs.
text = text.replace(/\$.*\$/g, "meow");
I'm trying to turn all text between two dollar signs into "meow" (placeholder).
EDIT:
Original question changed because the solution was too localized, but the accepted answer is useful information.
That's pretty close to what you want, but it will fail if you have multiple pairs of $text$ in your string. If you make your .* repeater lazy, it will fix that. E.g.,
text = text.replace(/\$.*?\$/g, "meow");
I see one problem: if you have more than one "template" like
aasdasdsadsdsa $a$ dasdasdsd $b$ asdasdasdsa
your regular expression will consider '$a$ dasdasdsd $b$' as a text between two dolar signals. you can use a less specific regular expression like
/\$[^$]*\$/g
to consider two strings in this example