开发者

replace string using regex

开发者 https://www.devze.com 2023-01-08 15:13 出处:网络
I have text inside \"textarea\" and I was trying to r开发者_如何学Pythonemove the text between: <textarea></textarea> using replace function with some regex. here is what I did so far:

I have text inside "textarea" and I was trying to r开发者_如何学Pythonemove 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>');


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消