views:

47

answers:

3

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

+1  A: 

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.

Pointy
I really don't know how to use regex with javascript.it seems like different from PHP.
ermac2014
Yes, it's different. Not a *lot* different, but different.
Pointy
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
+1  A: 

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

Eric Wendelin
thanks man this one works like charm..really appreciated :)
ermac2014
You're welcome :)
Eric Wendelin
The `m` modifier only matters if there are anchors (`^` or `$`) in the regex.
Alan Moore
A: 

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.

unigg