Skip to content
Advertisement

Check String against list of characters and replace it dynamically – Regex

I’m trying to find a solution for this matter. I have list of characters that needs to be replaced with particular character that is mapped with the original character.

Ex : I have character map which hold the characters and it’s replacement value. Character Map :

JavaScript

String that needs to be replaced : String hello = "Hello& World^"; I want replace hello string with the values in the map. This map is created from the property file and it is dynamic.

Can I achieve this by a regex ? Can I achieve this without iterating the character map ?

Advertisement

Answer

You may use this code:

JavaScript

Output:

JavaScript

Details:

  • In main regex we match \W which will match any non-word
  • We extract value of each matched non-word character from characterMap or if that key is not found we get same character back.
  • We call .replaceAll("\\", "$0$0") on extracted value to get right escaping (assuming values are just using single escaping). $0 is the complete string we match in regex here which is \\ and by using $0$0 we make it \\\\.

Code Demo


Another optimized way of doing this is to construct regex using keys of your map like this:

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