Help with RegEx

A swapping-ground for Regular Expression syntax

Help with RegEx

Postby tfonias74 » Mon Aug 12, 2019 8:06 am


I am trying to figure out how the regex replace works..
I am trying to rename the end character of any work that finishes with it to another. For example I need to replace s (when it is the last char on a word) to S, all other s should remain unchanged.
The above is an example, in my case the rename takes place with Greek filenames were the middle s is "?" and the tail s is "?".
I use as regex: (.*)(\w*?)(.*) and repl: \1\2?\3 and it matches one occurrence (I need also to change the char, in my results I get both of them at the end ex ....sS).
Is there a way to much any number of occurrences?

Thanks in advance.
Posts: 1
Joined: Mon Aug 12, 2019 7:57 am

Replace last word character

Postby RegexNinja » Fri Jun 05, 2020 12:50 pm

The best way is to match your last-word-char with a following space, or at EndName.
To replace s as a last-word-char with S

That replaces the last s with S whenever its a last-word-char.
You have to run the regex several-times to replace several-occurences.

With javascript, you can globally replace them all in 1-run:
newName=name.replace(/s( |$)/g, 'S$1')

Results like:
asss bsss csss dsss.txt ---> assS bssS cssS dssS.txt
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Return to Regular Expressions