Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
磅符号之间的Javascript正则表达式匹配_Javascript_Regex - Fatal编程技术网

磅符号之间的Javascript正则表达式匹配

磅符号之间的Javascript正则表达式匹配,javascript,regex,Javascript,Regex,快点。我有一个字符串:#用户9#我还活着我想退出“用户9” 到目前为止,我正在做: if(variable.match(/\#/g)){ console.log(variable): } 但是输出仍然是整行。使用.split(),以便取出所需的项目 var variable = variable.split("#"); console.log(variable[1]); .split()将字符串转换为数组,第一个变量作为分隔符 当然,如果您只想单独使用

快点。我有一个字符串:
#用户9#我还活着我想退出“用户9”

到目前为止,我正在做:

    if(variable.match(/\#/g)){
            console.log(variable):
    }
但是输出仍然是整行。

使用
.split()
,以便取出所需的项目

var variable = variable.split("#");
console.log(variable[1]);
.split()
将字符串转换为数组,第一个变量作为分隔符

当然,如果您只想单独使用regex,您可以:

console.log(variable.match(/([^#]+)/g));
这将再次为您提供一个项目数组,但较小的一个,因为它不使用散列之前的空值作为项目。此外,如@Stephen P所述,您需要使用捕获组(
()
)来捕获所需的项目。

使用

.split()
,以便取出所需的项目

var variable = variable.split("#");
console.log(variable[1]);
.split()
将字符串转换为数组,第一个变量作为分隔符

当然,如果您只想单独使用regex,您可以:

console.log(variable.match(/([^#]+)/g));

这将再次为您提供一个项目数组,但较小的一个,因为它不使用散列之前的空值作为项目。此外,正如@Stephen P所述,您需要使用一个捕获组(
()
)来捕获您想要的项目。

尝试更多类似的方法

var thetext="#user 9#I'm alive!";
thetext.match(/\#([^#]+)\#/g);
您需要引入一个捕获组(括号中的部分)来收集磅符号之间的文本

您可能希望使用
.exec()
而不是
.match()
,具体取决于您正在执行的操作。
还有一个问题是“如何在javascript正则表达式中访问匹配的组?”

尝试以下内容

var thetext="#user 9#I'm alive!";
thetext.match(/\#([^#]+)\#/g);
您需要引入一个捕获组(括号中的部分)来收集磅符号之间的文本

您可能希望使用
.exec()
而不是
.match()
,具体取决于您正在执行的操作。 另请参见SO问题“如何在javascript正则表达式中访问匹配的组?”