开发者

Use Match and then Replace in JavaScript Regex

开发者 https://www.devze.com 2022-12-15 07:56 出处:网络
Consider: <% content = \"\"+Request.Form(\"completehtml\")+\"\"; 开发者_如何学编程contentmatch = content;

Consider:

    <% 
        content = ""+Request.Form("completehtml")+"";
        开发者_如何学编程contentmatch = content;
        contentmatch = contentmatch.match(/(<div class="content">[\s\S]+?)(?=[##])/ig); 
    %>

If I get the above match and it gives me some HTML: is it possible to find text in the match and to replace it again?

    contentmatch = contentmatch.replace(/test/ig, 'working');

The problem I get with the replace is, that Internet Explorer says it is not supported. What is the reason?

    <% 
        content = ""+Request.Form("completehtml")+"";
        contentmatch = content;
        contentmatch = contentmatch.replace(/>\s+?</ig, '><'); 
        contentmatch = contentmatch.match(/(<div class="content">[\s\S]+?)(?=[##])/ig); 
    %>

OK it seems like I figured it out partly - If you do the replace before the match it seems to work, if you do it after the match is does not seem to work.

Is it possible to do a replace after the match?


Your line:

contentmatch = contentmatch.match(/(<div class="content">[\s\S]+?)(?=[##])/ig);

is replacing the contentmatch variable with the result of the match method. That result is an Array. Arrays do happen to have a replace method (in FF), but it does something different than the String method, which is what you want.

0

精彩评论

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