Skip to content Skip to sidebar Skip to footer

Regex: Matching A Character And Excluding It From The Results?

I am trying to find the first word of every paragraph using a regex in JavaScript. I am doing so with the following: function getFirstWordInParagraph(content) { return content.ma

Solution 1:

There's a special construct for "position at the start of a line": The ^anchor (if you set the MULTILINE option):

functiongetFirstWordInParagraph(content) {
  return content.match(/^\S+/gm);
}

You don't need the i option since nothing in your regex is case-sensitive.

This solution will also find the word at the very start of the string.

Post a Comment for "Regex: Matching A Character And Excluding It From The Results?"