This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
My goal is to replace all tags by [src] only
hello <img class="gif" src="https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif" title="1"> hello
world <img class="gif" src="https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif" title="2"> hello2
by
[https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif] [https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif]
https://regex101.com/r/ZPOCCe/1
(My regex works fine ONLY where there is a line between the 2 images. It looks like it is greedy)
Any idea ?
You can use .*? (add a question mark after) to make the regex lazy instead of greedy (just .*).
Related
This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed 2 years ago.
I want to use js syntax to have diffrent URL for each image. Robohash website give us random robot image if we use diffrent URL ending, in that way i want to do below, but looks like ${props.id} is not treated as syntax but just as a part of URL, so im gets the same image for all.
<img alt='robots' src={"https://robohash.org/${props.id}"} />
<img alt='robots' src={`https://robohash.org/${props.id}`} />
You should use template string using backticks:
<img alt='robots' src={`https://robohash.org/${props.id}`} />
This question already has answers here:
Parse an HTML string with JS
(15 answers)
Closed 3 years ago.
Regex expression is working when the content is in the same line. But it does not work if the same content is in multiple line. Please identify a mistake
Regex:
<([^\s]+).*?id="opensource".*?>(.+?)<\/\1>
It works with content in same line:
<article id="opensource">Cabby </article>
It does not works with content in different line:
<article id="opensource">
Cabby
</article>
You need to account for the whitespace characters.
<([^\s]+).*?id="opensource".*?>(?:\s+)?(.+?)(?:\s+)?<\/\1>
Here is a working example: https://regex101.com/r/OQH7A5/1
This question already has answers here:
Parse an HTML string with JS
(15 answers)
Closed 3 years ago.
Take the following:
<span class='exclass'>1.</span> Lorem Ipsum <span class="exclass2" title="This is an example">*</span>
I am trying to create a regex expression that will select:
<span class='exclass'>1.</span>
AND
<span class="exclass2" title="This is an example">*</span>
Regex expressions like ^(<span(.*)<\/span>)$ select all of the text.
What expression will select the two complete tags and ignore the loose text between them? (Lorem Ipsum)
Regex is not good way to find HTML tags. But this should work for you-
<\s*span[^>]*>(.*?)<\s*\/\s*span>
DEMO: https://regex101.com/r/vbLN9L/6
This question already has answers here:
What to do Regular expression pattern doesn't match anywhere in string?
(8 answers)
Closed 8 years ago.
I want to use JavaScript to match ALL content between <div class="class-test"> and </div>. What RegExp should I use?
The complicated part is, there might be some more HTML tags inside it. For example:
<div class="class-test">
<div> this is a sub div </div>
<p>this is a p</p>
</div>
BTW, The HTML is not real HTML but just pure text. So it's not possible to 'parse' it. I am actually using Node.JS to modify a HTML file.
Thanks.
You can do this easily with a module like cheerio:
var cheerio = require('cheerio');
var $ = cheerio.load('<div class="class-test"><div> this is a sub div </div><p>this is a p</p></div>');
var content = $('div.class-test').html();
console.log(content);
This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 8 years ago.
I have text that I want to remove and replace. I've have two tags which it lies between:
<!--start--></span>
</p>
<p id="nameIs" style="font-size: 12pt;">Dear Mr Johnstone,</p>
<p style="font-size: 12pt;">400 Isle Road</p>
<p style="font-size: 12pt;">Here Road</p>
<p style="font-size: 12pt;">KP33 7OL</p>
<p><span style="font-size: 12pt;"><!--end-->
I wan to to replace the text but I can't make the regex work. It seems not to find the text:
var textholder2 = textholder.replace(/<!--start-->.*<!--end-->/, idToUseIs)
idToUseIs is the replacement text.
thanks
textholder.replace(/<!--start-->(.|\n|\r)+<!--end-->/g, idToUseIs)
You dont need regex for fixed string.
Just use substring and simple maths.
var tok1 = "<!--start-->"
var tok2 = "<!--end-->"
textholder2 = textholder2.substring(0, s.indexOf(tok1)) +
textholder2.substring(textholder2.indexOf(tok2)+tok2.length