Skip to content Skip to sidebar Skip to footer

Javascript: Remove String Punctuation And Split Into Words?

Sorry if this has been asked before, but I'm trying to get an array of words from a string like this: 'Exclamation! Question? \'Quotes.\' 'Apostrophe'. Wasn't. 'Couldn't'. \'Didn't

Solution 1:

That would be tricky to work around your own solution but you could consider apostrophes this way:

sentence = `"Exclamation! Question? \"Quotes.\" 'Apostrophe'. Wasn't. 'Couldn't'. \"Didn't\"."`;
console.log(
    sentence.match(/\w+(?:'\w+)*/g)
);

Note: changed quantifier from ? to * to allow multiple ' in a word.

Solution 2:

@revo's answer looks good, here's another option that should work too:

const input = "Exclamation! Question? \"Quotes.\" 'Apostrophe'. Wasn't. 'Couldn't'. \"Didn't\".";
console.log(input.toLowerCase().match(/\b[\w']+\b/g));

Explanation:

  • \b matches at the beginning/end of a word,
  • [\w']+ matches anything that's either letters, digits, underscores or quotes (to omit underscores, you can use [a-zA-Z0-9']instead),
  • /g tells the regex to capture all occurrences that match that pattern (not just the first one).

Post a Comment for "Javascript: Remove String Punctuation And Split Into Words?"