Following the idea of parsing with regular expression, it could be like this:
var str = '4354543/sfd f^^ <textarea id="text">Hello World! 34% #_2@.;/°? </textarea> fr fdgdf //fdg3';
matchs = str.match(/\<textarea[\s\S]*?id\=\"text\"[\s\S]*?\>([\s\S]+?)\<\/textarea\>/);
console.log(matchs[1]);
In short, I seek the opening of the tag textarea
that has the id="text"
, followed by a search group, which will be the index 1
match, followed by closing the tag textarea
.
The character class [\s\S]
is a "trick" that finds anything EVEN. Cat jump very explained by @hkotsubo. This will not break the search if you have line break and etc.
I am not going to address everything that has been used in the regular expression, as a better solution is acceptable which I am going to present:
var str = (
'<textarea id="um_id_qualquer">Lorem, ipsum dolor sit amet consectetur adipisicing elit.</textarea>'
+'<textarea id="2_id_qualquer">Dignissimos ducimus quas illo a expedita pariatur maxime magni,</textarea>'
+'<textarea id="tres_id_qualquer">amet sint laborum eveniet, quam,</textarea>'
+'<textarea id="text">Hello World! 34% #_2@.;/°? </textarea>'
+'<textarea id="4_id_qualquer">recusandae enim iste delectus quidem! Iusto, at amet!</textarea>'
);
var parser = new DOMParser().parseFromString(str, "text/html");
console.log(parser.getElementById('text').innerHTML);
As the str
is a valid HTML, just create a DOMParser
and use the methods of the DOM itself.
I understood that you decided to use Regex. But it would not be better to use the method
getElementById()
, since its string is an HTML page?– LipESprY
And how would I use this method with a string ?
– JeanExtreme002
I will prepare an answer with the 2 methods.
– LipESprY
There is one more thing, I will use this for an application running on Node. But still, leave the answer with the method
getElementById
if possible, because I was curious about it.– JeanExtreme002
All right. I’ll just use javascript.
– LipESprY
Easier
<textarea[^>]*>([^<]*)
probably (if you have to appeal this much, to the point of needing Regex, of course)– Bacco
I think it’s important to add the tag
node
the question, since the Node does not have the native API for DOM manipulation, which changes quite the possible answers– Costamilam
Domparser or qq other API is much more guaranteed, as HTML just changes a little to break the regex. For example, if the textarea is inside comments (
<!-- <textarea> etc -->
), regex does not detect (and a regex to recognize HTML comments is very complicated), since Domparser ignores correctly (it’s just one example, there are several other cases that make regex not feasible depending on the case) - finally, it is always worth reading here and here– hkotsubo
@Costamilam the problem is to invalidate any of the answers. I don’t even know what is best in this case.
– Bacco
@Bacco would be better to have been created with the tag, but anyway, I still find it useful to keep the other answers, since there is no reason to create a new question for when you want the same, but in the browser
– Costamilam
Oops! You’ve heard of Cheerio? You can load your HTML into it and persist with queries to be able to extract the data you want. The documentation is very simple and you will probably have a very nice record.
– Jorge Linhares
Just to complement (and make a small "jabá"), I just answered just about that (using a parser versus regex), including showing some examples to better illustrate why the solution with regex is worse...
– hkotsubo