[英]How to match two characters with regex
這是我想在文本區域內匹配的內容:
@
+在調用@
並在其旁邊添加一個字符作為術語時執行用戶自動查找操作的任何字符。
這是textarea
<textarea></textarea>
這是js:
function strpos (haystack, needle, offset) {
var i = (haystack + "").indexOf(needle, (offset || 0));
return i === -1 ? false : i;
}
var dauto=function(){
if(strpos($(this).val(),"@ "+reg_exhere)!==false){
alert("match found");
}
}
$("textarea").bind("keyup",dauto);
$("textarea").bind("keydown",dauto);
現在,我真的不知道該在其中使用什么作為正則表達式,再加上我也不知道在其中使用它是否會有所幫助,因為它將使查找@之前的任何字符都松散,因此以下字符串將返回true :
@ mystring
因為我只需要找到:
@anycharacterhere
嘗試過
$(this).val().match(/^\+@\*+$/);
將String#match()
與簡單的正則表達式/@(\\w+)/
var dauto = function(){
$( '#capture' ).html($( this ).val().match(/@(\w+)/)[1]);
}
$("textarea").bind("keyup",dauto);
$("textarea").bind("keydown",dauto);
使用以下HTML塊進行測試
<textarea></textarea>
<p id="capture"></p>
在JsFiddle.net上的工作演示(嘗試在那里 輸入“ hi @username”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.