2

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks man this one works like charm.. really appreciated :)
The m modifier only matters if there are anchors (^ or $) in the regex.
1

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.

3 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.