我真的很难让正则表达式正常工作。我所想做的就是捕获一个字符串及其尾部空间,并将其作为数组的一个条目。
例如:
"RegExr #was created by gskinner.com, and is proudly hosted by Media Temple.
编辑表达式&文本以查看匹配项">
["RegExr ", "#was ", "created ", "by ", gskinner.com " ... "& " ... "matches. " etc...]
我正在尝试在React应用程序中执行以下操作:
console.log("matches are: ", message.match(/(w+s|#w+s|s )/));
return message.match(/((w+ ?))/);
// return message.split(" ");
我发现split函数给我带来了一些奇怪的问题,所以我只想让matches函数返回我上面指定的结果数组。
当我注销一些尝试时,无论我尝试什么,我都会得到奇怪的结果,比如:
例如
matches are: (2) ["Hey ", "Hey ", index: 0, input: "Hey Martin Im not sure how to make the font in this box bigger #ZX81", groups: undefined]
有人能帮我吗?它真的阻止了我的应用程序的一些进展。
要获得所有带有任何相邻空白的非空白字符块,可以使用
message.match(/s*S+s*/g)
请参阅regex演示。g
标志将提取的所有匹配项
s*
-零个或多个空白字符S+
-一个或多个非空白字符s*
-零个或多个空白字符
查看JavaScript演示:
const message = "RegExr #was created by gskinner.com, and is proudly hosted by Media Temple. Edit the Expression & Text to see matches.";
console.log(message.match(/s*S+s*/g));