This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 5 years ago.
I am trying to read a string formatted like
<test>input</test>\n <another>input</another>
My regex works for the test tagged input, but ignores the another tagged input. If I wrap the entire regex in parenthesis and use the brackets {} to specify how many times, then it only saves the last match case. How can I catch and save all match cases?
My regex:
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/
Result contents of match:
<test>input</test>\n <another>input</another>
<another>input</another>
another
input
/input
Add a g Modifier so specify that it is global (allows for multiple results)
So change your regexp to (notice the g in the end)
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/g
Related
This question already has answers here:
How can I match a pipe character followed by whitespace and another pipe?
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I'm using this JS regex text.match(/|(https:.+?path.+?)|/)[1] to get a regex of a URL that is in between pipe | characters but it's not working.
The text is ||https://url.com/path/value|| but I can't seem to extract the URL from it. I need to have path in the middle to identify this particular URL since there are other URLs in the file.
It doesn't have to be a URL that I'm extracting. I mainly would like to know how to extract something from between a pair of characters (| in this case).
You need to escape the pipe ("|") characters:
text.match(/\|(https:.+?path.+?)\|/)[1]
Pipe is a special character that basically means "or". https://www.regular-expressions.info/alternation.html
To grab everything between the two sets of || then you could use this regex:
text.match(/\|\|(.*)\|\|/)
The first part \|\| matches the characters || literally.
The next part (.*)matches any character zero or more and groups the result.
The last part \|\| matches the closing characters || literally.
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 am parsing HTML with this regex in javascript for selecting the attribute values on HTML elements:
/(\".+\")/g
It works fine when there is a single attribute, but when there are multiple attributes, like so:
See How
it is matching from the first quote on the first attribute to the last quote on the second. How can I get the regex to identify the attribute values as separate matches?
The matching is greedy by default. Try this:
/(\".+?\")/g
You have to stop the greediness of that regular expression by placing ?,
/(\".*?\")/g
Also you have to use * at this context instead of +. Because if you have an empty attribute then it would match the next attribute also along with the attribute name.
This question already has answers here:
Trim specific character from a string
(22 answers)
Closed 6 years ago.
I have a requirement to remove extra underscores from a string. The condition is if they occur at the very end of the string only.
As an example, we have DELL_ and DELL__ that needs to be changed to DELL.
I was considering using str.replace but I need to match cases specifically if it occurs at the end of the string and not all occurrences in that string. Also, I only want to run this script IF it detects the extra underscores.
I need to have some logic such as IF ( hasExtraUnderscores ) { remove extra underscores }
How can I do this in javascript?
NOTE: We are unable to use JQuery and need to do this in native javascript if possible.
Try this
var str = 'DELL_'
alert(str.replace(/_+$/,'');
This question already has answers here:
Regex for everything before last forward or backward slash
(3 answers)
Closed 9 years ago.
I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?
I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:
var stringVariable = 'America/Argentina/Buenos_Aires',
text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I found this question/answer:
Use JavaScript regex to replace numerical HTML entities with their actual characters
I just need to replace the one entity though. How can I match that specific pattern with a regex?
I don't know much about regex so I've done this:
.replace('–', '–')
But it obviously only replaces the first instance.
Thanks,
Thomas
The replace method only replaces the first occurance when you are using a string. Use a regular expression, so that you can specify the global flag g:
.replace(/–/g, '–')
.replace(/–/g, '–')
the g flag means global so it replaces all instances.