Danielok1993
Danielok1993

Reputation: 359

JavaScript Split around curly braces

I have a string format inside of a string variable:

"{0} Hello World {1}"

I need to split it into something like this:

"{0}","Hello World","{1}"

From what I have tried the best I could get was:

"","0"," Hello World ","1",""

I have tried converting the examples from Regex Split Around Curly Braces but it did not work, the split there either deleted everything or kept the spaces and removed the {}.

So, my questions is the same as in the other article, how do I keep the braces {} and remove spaces after and before them not in between the words?

Upvotes: 7

Views: 6267

Answers (5)

Arathi Sreekumar
Arathi Sreekumar

Reputation: 2574

var myString = "{0} Hello World {1}"
var splits = myString.split(/\s?{\d}\s?/);

which would return ["", "Hello World", ""],

var count = 0;
for (var i=0; i< splits.length; i++) {
    if (splits[i] === "") {
        splits[i] = "{" + count + "}";
        count++;
    }
}

now splits would have what you need.

Upvotes: -1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

You can use Split with a regex having capturing group(s):

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array.

var re = /\s*(\{[0-9]+\})\s*/g;
var splt = "{0} Hello World {1}".split(re).filter(Boolean);
alert(splt);

Regex explanation:

  • \s* - Any number of whitespaces
  • (\{[0-9]+\}) - A capturing group that matches:
    • \{ - a literal {
    • [0-9]+ - 1 or more digits
    • \} - a literal }
  • \s* - Any number of whitespaces

filter can help get rid of empty elements in the array.

filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a true value or a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

Array elements which do not pass the callback test are simply skipped, and are not included in the new array.

Upvotes: 7

wonderb0lt
wonderb0lt

Reputation: 2043

You can just filter out the empty strings if you want:

var s = "{0} Hello World {1}";
s.split(/({\d+})/).filter(Boolean);

which returns

["{0}", " Hello World ", "{1}"]

Upvotes: 0

Thiago Souza
Thiago Souza

Reputation: 135

You can do like this:

var myregexp = /({.*?})(.*?)({.*?})/im;
var match = myregexp.exec(subject);
   if (match != null) 
   {
     result1 = match[1];
     result2 = match[2];
     result3 = match[3];
   }

Upvotes: 0

karthik manchala
karthik manchala

Reputation: 13640

You can use the following:

\s*({\d+})\s*

JS Code:

var regex = /\s*({\d+})\s*/g;
var split_string = "{0} Hello World {1}".split(regex).filter(Boolean);
alert(split_string);

Upvotes: 5

Related Questions