Text between two dollar signs JavaScript Regex [duplicate] - javascript

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

Related

Split text by urls [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 3 years ago.
I need to split a given text by urls that it might contain, while keeping the urls-separators in the resulting array.
For example splitting this text:
"An example text that contains many links such us
http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and
link-4.com."
would result into this array:
["An example text that contains many links such us ", "http://www.link1.com", ", ", "https://www.link2.com/path?param=value", ", ", "www.link3.com", " and ", "link-4.com", "."]
I tried to use String.protoype.split() with a regular expression, but it's not working as it contains unwanted parts of the urls themselves:
var text = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
console.log(text.split(/((https?:\/\/)|([\w-]{2,}[.])+([\S]{2,})[^\s|,!$\^\*;:{}`()])+/ig));
EDIT
This question is different than the suggested ones, my purpose is not to check if a url is valid or not, but to find a regular expression susceptible to be used in the split method, and that splits correctly the text.
As for splitting a text by regex, it is already used in the snippet sample. What is proposed in the suggested question is more general, and what I am looking for is more specific to urls.
it's not ideal and it would be hard to find or create perfect regex for it that you going to test all cases but you can quickly write something like this:
var text2 = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
text2
.split(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig)
.filter(Boolean)
.filter((x)=>{ return x.indexOf('.')>0 })

Regex. Escape group? [duplicate]

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[.+()[]]

Regex capture everything between two tags across multiple lines [duplicate]

This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 7 years ago.
I have this regex in Ruby: http://rubular.com/r/eu9LOQxfTj
/<sometag>(.*?)<\/sometag>/im
And it successfully matches input like this:
<sometag>
123
456
</sometag>
Which would return
123
456
However, when I try this in javascript (testing in chrome), it doesn't match anything.
Does javascript's multiline flag mean something else?
I want to capture everything non-greedily between two given tags.
How can I accomplish this in javascript using regex? Here is a Debuggex Demo
<sometag>(.*?)<\/sometag>
This is not XML parsing.
Javascript does not support multiline expressions when using . alone. You have to use [\s\S] in place of . so an example that satisfies what you want would be:
var x = "<sometag>\n\
123\n\
456\n\
</sometag>";
var ans = x.match(/<sometag>([\s\S]*?)<\/sometag>/im).pop();
// ans equals " 123 456"
note that you still need the m modifier.

Developing a Regex pattern for an email [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 9 years ago.
I have no experience with regular expressions in java script, but I need to derive a pattern for
FMLast1234#ung.edu. There only needs to be a pattern for FMLast1234 because #ung.edu needs to remain the same. I am not sure if I just do this \b[a-z][a-z][a-z][0-9] or what.
Does there need to be a range for each character in the pattern? I need to check for variations of the pattern FMLast1234 not just a random assortment of characters and numbers.
/[a-zA-Z0-9]#ung.edu/.test("123#ung.edu") or
if(emailString.split('#')[1] === "ung.edu")
Edit : As per plalx comment here is my answer
/^\w+#ung.edu$/.test("aaa123#ung.edu")

Ignoring line breaks in Javascript regex [duplicate]

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/)

Categories

Resources