如何从<textarea>

时间:2015-11-04 14:02:52

标签: javascript jquery html css regex

&lt; p&gt;附加小提琴中的代码会删除&lt; code&gt;&lt; textarea&gt;&lt; / code&gt;中的所有网址。&lt; / p&gt; &lt; p&gt;代码是否可以删除所有网址,除非网址在括号内 - &lt; em&gt;例如&lt; / em&gt; (google.com) - 在&lt; code&gt;&lt; textarea&gt;&lt; / code&gt;内动态&LT; / p为H. &lt; p&gt;如果可能的话,我会很感激,因为我不熟悉编码。&lt; / p&gt; &lt; p&gt;&lt; div class =&#34; snippet&#34;数据琅=&#34; JS&#34;数据隐藏=&#34;假&#34;&GT;&#13; &lt; div class =&#34; snippet-code&#34;&gt;&#13; &lt; pre class =&#34; snippet-code-js lang-js prettyprint-override&#34;&gt;&lt; code&gt; $(function(){&#13;   $(&#34; #txtarea&#34;)。keyup(function(){&#13;     的console.log(THIS.VALUE);&#13;     this.value = this.value.replace(/(https?:\ / \ /)?([\ da-z \ .-] +)\。([az \。] {2,6})([\ / \ w \ .-] *)* \ /?/ mg,&#39;&#39;);&#13;   })&#13; });&LT; /代码&GT;&LT; /预&GT;&#13; &lt; pre class =&#34; snippet-code-html lang-html prettyprint-override&#34;&gt;&lt; code&gt;&lt; script src =&#34; https://ajax.googleapis.com/ajax/库/ jquery的/ 1.11.1 / jquery.min.js&#34;&GT;&LT; /脚本&GT;&#13; &#13; &lt; textarea id =&#34; txtarea&#34;占位符=&#34;如何阻止http进入textarea?&#34;&gt;&lt; / textarea&gt;&lt; / code&gt;&lt; / pre&gt;&#13; &LT; / DIV&GT;&#13; &LT; / DIV&GT;&#13; &LT; / p为H. &lt; p&gt;&lt; a href =&#34; http://jsfiddle.net/95b2msaj/9/"的rel =&#34; nofollow的&#34;&GT;小提琴&LT; / A&GT;&LT; / p为H.

1 个答案:

答案 0 :(得分:1)

修改:请使用以下代码(包含单个网址的字符串)。

   $(function(){
    var urlmatch = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/gm, protomatch = /\((https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?\)/gm;
    $("#txtarea").keyup(function(){
        if( this.value.match(protomatch) && this.value.charAt(0) == '(' && this.value.charAt(this.value.length-1) == ')' ){
            this.value = this.value;
        }
        else if(this.value.match(urlmatch) && !this.value.match(protomatch) && this.value.charAt(0) != '(' && this.value.charAt(this.value.length-1) != ')'){
            this.value = this.value.replace(urlmatch, '');
        }
    })
});

修改:它会检查并删除整个字符串中的所有下一个网址(多个网址)。

       $(function(){
                $("#txtarea").keyup(function(){    
                    var urlmatch = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, newA = [], a = this.value.split(' '); 
                $.each(a, function (i, j) {//j = $.trim(j); 
                if((j.match(urlmatch) && j.charAt(0) == '(' && j.charAt(j.length-1) == ')') != false) {
                newA.push(j)
                }
                console.log(j, '->', j.match(urlmatch) && j.charAt(0) == '(' && j.charAt(j.length-1) == ')'  ) })

                var o = newA.join(" ")
                this.value = o;
                })
            });

为此找到更新的fiddle。干杯!

相关问题