0

I would like a regex to match the first letter of every word in a string.

I am using the XRegExp Javascript library.

I am actually trying to copy a regex I have in my server-side C# code (seemingly working fine):

new Regex(@"\b[\p{L}]|(?<=[-])[\p{L}]").[omitted code]

Starting small, I have tried the following:

XRegExp.exec("foo bar", XRegExp("\\b\\p{L}", "g"))

I expected ["f","b"] (using the 'all matches' flag (g), but only got: ["f"]

What am I doing wrong?

4

1 回答 1

0

你没有得到两者fb因为exec()在每次调用中都会连续找到匹配项。

它通常用于while循环查找所有匹配项。

如果您使用的是本机库,那么我会建议您使用string.match()一次查找所有匹配项。

检查exec()您的情况的文档,然后寻找类似于 的方法string.match()

于 2013-11-03T14:00:05.793 回答