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[.+()[]]
Related
This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
I need to remove everything before the third forward using regex so that for example https://stackoverflow.com/questions/ask becomes /questions/ask I'm not the greatest when it comes to regular expressions so your help would be much appreciated.
This is what I have so far https://regex101.com/r/qQ2dE4/498
The code I currently have is but want to use regex:
url.substring(url.indexOf('\/') + 3)
Use this:
(?<=.*\/.*\/.*\/).+
Demo
Explanation:
(?<= - its positive look behind, in any position that's pattern inside it is find matching start from this position to forward.
.*\/.*\/.*\/ - it is used inside the positive look behind, it cause matching start after the position that behind that position is 3 forward slashes
.+ - match one or more of from anything
Edit:
From #JaromandaX's comment, this can also be used (and honestly I think it more readable and simper):
(?<=(?:.*?\/){3}).+
I understand the questions asks for regex but the need is unclear - if all you need is the path name there are better tools in the toolbox (for anybody not working on a homework exercise).
const url = 'https://stackoverflow.com/questions/ask'
console.log(new URL(url).pathname)
This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)
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:
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