Skip to content
Advertisement

Add a space after a word if it’s at the beginning of a string or if it’s after one or more spaces, and at the same time it must be at end or before n

JavaScript

How to obtain this outputs from those inputs?

JavaScript

Note that for examples 4, 5, 6 and 7 the regex should not make any changes, since after the word there is already a space placed, or because in the case of "uno", the word "un" is not at the end of the sentence, or in the case of "treinta yun" the substring "un" is not preceded by one or more spaces.

Advertisement

Answer

If you want to use regex, you can use bun$, which checks that the last whole word in the string is un, and that there is nothing after it in the string. If that is the case, a space is added to the end of the string:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement