regex is not matching content in multiple line in javascript [duplicate] - javascript

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

Related

How i can change text in HTML with Javascript [duplicate]

This question already has answers here:
Find and replace specific text characters across a document with JS
(14 answers)
Closed 11 months ago.
<a href="/knowledgebase/category/community?language=tr" class="Category-Item">
<h4><span><i class="fas fa-comments"></i></span> Community</h4>
<p></p>
<span class="MoreArticles">8 Makaleler</span>
</a>
I want find and replace text under a class.
For example; Finding "Community" text and change to "Topluluk".
I writed a javascript language and i need do this for non-multilangual application.
Before:
<a href="blabla" class="**Category-Item**"><i class="blabla">Community</i>
After:
<a href="blabla" class="**Category-Item**"><i class="blabla">Topluluk</i>
from what i understood , you can select the element then change the inner text. if that's not what you ment please leave a comment.
let text = document.querySelector('h4');
text.innerText = "changed";

Javastript syntax in quotes [duplicate]

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}`} />

Select text between 2 complete span tags using regex [duplicate]

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

JS and regex: replace all img by their [src]? [duplicate]

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 .*).

How to use RegExp to match all characters between HTML tags? [duplicate]

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

Categories

Resources