Desolator
Desolator

Reputation: 22779

replace string using regex

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

Upvotes: 2

Views: 924

Answers (3)

unigg
unigg

Reputation: 464

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.

Upvotes: 0

Eric Wendelin
Eric Wendelin

Reputation: 44379

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

Upvotes: 2

Pointy
Pointy

Reputation: 413996

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.

Upvotes: 1

Related Questions