从textarea中提取文本,该文本位于@和#符号之间

时间:2015-12-23 06:12:57

标签: jquery regex textarea

我想从textarea中提取文本,该文本位于@和#符号之间,因为它使用的是正则表达式。

\B@(\w*)$/

但是,如果分隔符之间有空格,则无法得到预期的结果。

例如

"Welcome to the company @first name last name# We all wished."

我的输出应该是:

first name last name

2 个答案:

答案 0 :(得分:1)

您可以使用 /@(.*?)#/ 来匹配@#之间的任何内容



var str = "Welcome to the company @first name last name# We all wished.";
var res = str.match(/@(.*)#/)[1];
document.write(res);




Regular expression visualization

答案 1 :(得分:1)

尝试这个

var regex = /\@(.*?)\#/;
var str="Welcome to the company @first name last name# We all wished.";
var matched = regex.exec(str);
console.log(matched[1])