Separate Matches in 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 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.

Related

Remove everthing between two characters [duplicate]

This question already has answers here:
Strip HTML from Text JavaScript
(44 answers)
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 2 years ago.
How would you remove everything between all instances of brackets like in
var item = '<p>1. Get this <a title= "Static Review"> </a> more text </p>'
I've tried using the solution from How can I remove a character from a string using Javascript? with the global tag, formatted like : item = item.replace(/\/<.*>/, ''), but that just outputs nothing.
Really lost here
Just change your line of code by adding a ?:
item = item.replace(/<.*?>/g, '')
While .* is a greedy match, .*? is ungreedy. Greedy means "match as much as you can". Ungreedy means "match as few as possible". Thus <.*?> will stop at the first closing bracket, and do what you want.
The second change to your code: add the /g modifier. In Javascript, /g means "match all occurences", while without, the regex only matches the first occurrence.

Remove single quotes and last comma in jquery string using Regex [duplicate]

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks
try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match
There is an easy way, use the $ operator
.replace(/'|,$/g, '')
Can you please check replace(/'|(,)$/g," ") and it should work.

Javascript Regex Not Giving Multiple Results [duplicate]

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

Javascript remove extra underscores at the end of a string only [duplicate]

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(/_+$/,'');

How can I use regex to replace all $#8211; with – in javascript? [duplicate]

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.

Categories

Resources