Make a regex that spans over multiple lines? - javascript

I have looked at the flags and I cloudn't find what I am looking for. Basically if I am searching for:
aba
It should totally ignore the new lines, so the following things are valid:
a
b
a
a
b
a
ab
a
Edit: I am aiming at doing something a bit more elegant than putting \s? after every character in the regex (given that it is a constant if it is a range than I have no idea what so ever)

/a\s*b\s*a/
Place whitespace possibilities between each letter.

The simple case
For your example where the exact letters are aba, I would go with
a\s*b\s*a
See demo
The more intricate case
In a comment, you ask about an expression such as [a-z]{1,5}, where you presumably want to inject potential spaces between the letters. For this, I would go with
(?:[a-z]\s*){1,5}
See demo

:)) It's an interesting problem. For this situations I use another method.
First I remove all line ending chars:
someText = someText.replace(/(\r\n|\n|\r)/gm,"");
Use a normal regex

Related

Regex for - 'A,B','C'

I have written this regex -
([\s]*'[A-Za-z0-9_: ]*[\,]*[\s]*[A-Za-z0-9_: ]*\'[\s]*)[\,]*
But this is not handling the input - 'A,B' 'C' - In this the comma is missing, still its a perfect match.
Can anyone please help.
After giving this more thought, I think what you want is something more like this:
^(?<item>\'[a-zA-Z0-9,\s]+\')(\s*,(?&item))*\s*$
You're using an asterisk which will match zero instances. Try using + instead for the characters you want one or more of.
Please provide other examples that you'd expect to match. For this specific case, the following would match, but is very rigid and specific:
\'+[a-zA-Z]+\,\s*[a-zA-Z]+\'+\,\s*\'+[a-zA-Z]+\'+
Edit:
This is more in line with what I think you want:
^(\'[a-zA-Z]+(\,+\s*[a-zA-Z]+)*\'\s*\,*)*$

Regex match all except a pattern

I need a little help. I already tried to practice in several ways, but it didn't work as expected. For example, this one.
I want to match all single words except the pattern <br> in JS.
So I tried
(?!<br>)[\s\S]
(?!<|b|r|>)[\s\S]
The problem I have is, in the ?! quote, it's matching either the first word, < only, not the entire pattern <br>. In reverse, just <br> can match all <br> expect any other words. How can I let it know I want to match the entire word in the ?! quote?
Thank you so much!
Here is what I am trying.
The regular expression you are looking for might look like this:
([^>]|<(?!br>)[^>]+>)+(?=<br>|$)
It should work for any tag, try replacing br by p in the above pattern.
Regex101 link
However, It would be much easier and readable and faster to use:
content.split('<br>').filter(x => x.length)
Hope it helps.

RegEx to get ALL Strings between two Strings

I seem to have a love/hate relationship with RegEx in that I love how incredibly powerful it is, but at the same time, I don't quite understand all of the nuances of it yet.
I've got rather lengthy JSON feed that I need to parse and capture ALL of the matches between two specific strings. I've included a link to the regex101.com example with a few of the JSON results.
regex101.com Example
I'm trying to match every string between each /content/usergenerated and /jcr:content
...
I guess what I should really be trying to match is a string that starts with /content/webAppName/en/home and ends before /jcr:content
The path that I care about will always start with /content/webAppName/en/home
you have to use "positive look-ahead" that match a sequence of digits if they are followed by something
https://regex101.com/r/fU1iD1/4
Just wrap the two things you're looking to remove in parenthesis, and then remove them from the output. So...
(\/content\/usergenerated)(.*)(\/jcr\:content)
replaced by
/2
Which is everything in the middle of those two.
edit: Sorry, didn't look at your example :) - there was a deleted answer that said to add the g modifier, which looks like it works.
/content/usergenerated/content/webAppName/en/home([a-zA-Z/-]+)/jcr:content
This should work. It matches 3 out of 4 don't know why it doesn't match one of em. You could use exec() in a loop till it returns null and get hold of the object[1] which contains data for the first and only capture group.
all the best.
PS: I used gmi in options for the regex.

Need help in regex pattern

i have this Regex pattern
\=[a-zA-Z\.\:\[\]_\(\)\&\$\%#\-\#\!0-9;=\?/\+\xBF\~]+[?\s+|?>]
and i have this HTML
1.esc#xyz.com
2.johnross#zys.com
3.johnross#wen.com
Here the problem is,
I need to avoid first and second as it has white space as well and it is valid attributes.
But only the third one is working as it does't has white spaces.
means nothing should be selected with the above pattern.
here is direct link to test
http://regexr.com?31r61
Please help!
Thanks,
EDIT:
If you just want to match unquoted attributes, this should work:
[<\s]+[\w]+(=[^\"][^\s>]*)
Kind of inelegant but let me know if that does what you want.
Which pattern are you trying to match? All three? And if so, which portion? The subject or the email? If you're just trying to match the subject, try using this as the pattern to match:
\=\"mailto:[^?]*\?subject=([^\"]*)\"\>
That will return a match where the group is the subject itself.
That is a wicked character class....
why don't you try something a bit more reasonable. Try this...
\=".*?(?<!\\)"
that will match anything in the parenthesis after href if that's what you're trying to get. If you're looking for more than that, this regex can easily by modified.

Find uppercase substrings and wrap with acronym tags

For example replace the string Yangomo, Congo, DRC with Yangomo, Congo, <acronym>DRC</acronym>. There may potentially be mulitple uppercase substings in each string. I assume some form of regex?
Thanks.
Well, a really simple one might be:
var replaced = original.replace(/\b([A-Z]+)\b/g, '<acronym>$1</acronym>');
Doing this sort of thing always has complications, however; it depends on the source material. (The "\b" thing matches word boundaries, and is an invaluable trick for all sorts of occasions.)
edit — insightful user Buh Buh points out that it might be nice to only affect strings with more than two characters, which would look like /\b([A-Z]{2,})\b/.
Personally I would use PHP to explode the string, use a regex to find all uppercase letters /[A-Z]+/ and then use PHP to insert the tags (using str_replace).

Categories