1

这是我想在 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(/^\+@\*+$/);
4

1 回答 1

1

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”)

于 2013-08-11T15:42:55.657 回答