adot
adot

Reputation: 398

RegExp to find and replace all the spaces between two separate delimiters

I want to find all the white space between two separate delimiters and replace to effectively remove it.

For instance:

{First Value} where {Second Available Value} is greater than {Value}

I want the string to become:

{FirstValue} where {SecondAvailableValue} is greater than {Value}

I have little experience in regexp but this what I tried on a regex builder:

/{([^}]*)}/g

This however matches the sub strings (words in between the curl braces) including the delimiter

How can I match just the spaces inside the curly braces?

Upvotes: 2

Views: 275

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

We can try doing a regex replace with a callback function, targeting the following pattern:

\{.*?\}

That is, we will try to match every term contained in curly braces. This callback function can then remove all spaces.

var input = "{First Value} where {Second Available Value} is greater than {Value}";
console.log(input);
input = input.replace(/\{.*?\}/g, function(match, contents, offset, input_string) {
    return match.replace(/ /g, '');
});

console.log(input);

Upvotes: 4

Related Questions