up vote 1 down vote favorite

Hi I have text inside "textarea" and I was trying to remove the text between: <textarea></textarea> using replace function with some regex. here is what I did so far:

x = '<TEXTAREA style="DISPLAY: none" id=test name=test>teeeeessst!@#$%&*(LKJHGFDMNBVCX</TEXTAREA>';

x.replace('/<TEXTAREA style="DISPLAY: none" id=test name=test>.*</TEXTAREA>/s','<TEXTAREA style="DISPLAY: none" id=test name=test></TEXTAREA>');

thanks for your help :)

link|flag

50% accept rate
Is that <textarea> actually in the page? – KennyTM Jul 22 at 12:43
And what did you get ? – M42 Jul 22 at 12:47
problem solved :) check Eric's answer – ermac2014 Jul 22 at 13:08
then check this answer as solution. – Hippo Jul 22 at 13:53

3 Answers

up vote 1 down vote accepted

You'll probably want something like this:

x.replace(/(<textarea[^>]*>)[^<]+(<\/textarea>)/img, '$1$2');

This will replace things case-insensitively within multi-line strings and avoiding greedy matches of things like ".*"

link|flag
thanks man this one works like charm.. really appreciated :) – ermac2014 Jul 22 at 13:05
You're welcome :) – Eric Wendelin Jul 22 at 14:43
The m modifier only matters if there are anchors (^ or $) in the regex. – Alan Moore Aug 8 at 7:19
up vote 1 down vote

First problem is that you've got your regex inside quotes. It should just be /regex/ without quotes. Then you're going to have to put a backslash before the forward slash in the regex.

/<TEXTAREA style="DISPLAY: none" id=test name=test>.*<\/TEXTAREA>/

There's no regex flag "s", so I don't know what you thought it means but just drop it.

link|flag
I really don't know how to use regex with javascript. it seems like different from PHP. – ermac2014 Jul 22 at 12:52
Yes, it's different. Not a lot different, but different. – Pointy Jul 22 at 13:20
You mean there's no s flag in JavaScript regexes. To get the same effect we usually use replace the . with [\s\S], but in this case [^<] is more appropriate, as @Eric showed. – Alan Moore Aug 8 at 7:40
up vote 0 down vote

Similar to Eric's method, or use more general regexp.

var re =/(\<[^<]+\>)[^<]+(<\/[^<]+>)/;

x = x.replace(re, '$1$2');

You can use this tool to have a test. The result should be output to testarea.

link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.