Skip to content
Advertisement

Reverse a string except for the characters contained within { } with javascript

I need to reverse a string except the characters inside of “{}”. I know how to reverse a string but I’m not sure how to create the exception. Please help.

JavaScript

Advertisement

Answer

Or, maybe, this is what you want?

JavaScript

The RegExp /}w+{/g will find any string of characters and numbers (w+) that is enclosed by } and {. These patterns will exist after the whole string is reverse()-d initially. In the callback function to the String.replace() method the matched string will then be reversed again.

Advertisement