js技巧(三)

1.檢測瀏覽器,search的用法html

if(window.navigator.userAgent.search(/firefox/i)!=-1){
            alert('ff');
        }
        else if(window.navigator.userAgent.search(/chrome/i)!=-1){
            alert('gg');
        }
View Code

2.match的用法chrome

var str = "str 223 frfr 23 tg 4r56e";
        var re = /\d+/g;//g 全局匹配  全局指的是在當前字符串中進行所有數據的匹配。

        console.log(str.match(re));
View Code

3.repalce的用法數組

var str = "abcdeafAg";
        var n = str.replace(/a/ig,'T');//返回替換的數組 
        console.log(n);//TbcdeTfTg
        console.log(str);//abcdeafAg
View Code

4.過濾敏感詞彙瀏覽器

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <h1>敏感詞過濾</h1>
        <textarea id="txt1" cols="30" rows="10"></textarea><br>
        <input type="button" id="btn1" value="過濾"><br>
        <textarea id="txt2" cols="30" rows="10"></textarea>
    </body>
    <script>
        window.onload = function ()
        {
            var oTxt1 = document.getElementById('txt1');
            var oTxt2 = document.getElementById('txt2');
            var btn1 = document.getElementById('btn1');

            btn1.onclick = function (){
                var re = /fuck|傻吊|DDS|DSD/g;// | 或者
                oTxt2.value = oTxt1.value.replace(re,'***')
            }
        }
    </script>
</html>
View Code

5.去標籤ide

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>

    <body>
        <textarea id="txt1" cols="30" rows="10"></textarea><br>
        <input type="button" id="btn" value="提交"><br>
        <textarea id="txt2" cols="30" rows="10"></textarea>
    </body>
    <script>
        window.onload = function ()
        {
            /*
                html 標籤特色  <> 裏面有內容
                <>  /<>/g
                . 
             */
            var oTxt1 = document.getElementById('txt1');
            var oTxt2 = document.getElementById('txt2');
            var btn = document.getElementById('btn'); 
            btn.onclick = function ()
            {
                var re = /<[^<>]+>/g;
                oTxt2.value = oTxt1.value.replace(re,'')
            }
        }
    </script>
</html>
View Code
相關文章
相關標籤/搜索