Question

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?

Was it helpful?

Solution

You are not getting both f and b because exec() finds matches successively on each call.

It is usually used in a while loop to find all the matches.

If you were using the native library, then I would have advised you to use string.match() to find all the matches at once.

Check the documentation of exec() in your situation, and then look for a method similar to string.match().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top