'<'字符串中的字符忽略.replace()

时间:2015-08-18 03:00:36

标签: javascript regex str-replace

我想替换'<'之间包含文本的某个字符串和'>'。

有趣的是.replace()几乎可以在任何文本上正常工作,但是当'<'时包含在字符串中,它不会返回过滤后的字符串。

以下是一些测试它的简单代码:http://jsfiddle.net/gy0y90g5/

我尝试了什么:

仅限文字:

var res = str.replace('bijgevoegd>', "");

正则表达式:

var res = str.replace(/bijgevoegd>/g, "");

因为jsfiddle不会显示介于< >之间的任何内容我刚刚添加了一个'&gt;'在示例中。

任何人都知道这种奇怪的行为是什么?

1 个答案:

答案 0 :(得分:-1)

我做了这个,我不知道这是不是你想要的,但是在这里你好!

<html>
<head>
    <title>STACK OVERFLOW TESTS</title>
</head>
<body>
    <p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

    <p id = 'text'>< Something ></p>

    <input type = 'button' id = 'button' value = 'CLICK ME!'></input>

    <script>
        var button = document.getElementById('button');
        button.addEventListener('click', doTheMagic);

        function doTheMagic(){
            var paragraph = document.getElementById('text');
            paragraph.innerHTML = '< W3Schools >'

        }
    </script>
</body>
</html>
相关问题