Skip to content Skip to sidebar Skip to footer

How To Replace And Correct Json Path String With Regex?

I have a javascript array of strings, with modified json path just, like this: 55-fathers-2-married 55-fathers-2-name 55-fathers-2-sons 55-fathers-1-id 55-fathers-1-married 55-fat

Solution 1:

json.replace(/-/g, '.').replace(/(^|\.)([0-9]+)($|\.)/g, '[$2]$3');
  1. Replace the dashes with periods
  2. Search for all numbers within periods or at the beginning or end of a line. Surround those results with brackets. Then add the period ($3) after the bracket if necessary.

Solution 2:

You could do something like this:

str.replace(/([a-z]+)/gi, ".$1").replace(/(\d+)/gi, "[$1]").replace(/-/g, '');

Post a Comment for "How To Replace And Correct Json Path String With Regex?"