Search code examples
javascriptregexmatchpunctuation

JavaScript regular expression to match a word with any kind of punctuation at the end


I am trying to translate sentences into Pig-Latin using regular expressions. I need to select the words with any kind of punctuation at the end so that I can handle those cases differently.

For example, in "I think, therefore I am." I need an expression to match "think," and "am."

I have tried various approaches, such as word.match(/\w+[!?.:;]$/) with no results.


Solution

  • My guess is that you might be trying to write an expression, maybe a bit similar to:

    \w+(?=[!?.:;,])
    

    Demo

    const regex = /\w+(?=[!?.:;,])/gm;
    const str = `I think, therefore I am.
    Je pense; donc je suis!
    I don't think: therefore I am not?
    
    
    `;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }


    If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here