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 g...

问题:

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?


回答1:

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);

  • 发表于 2019-03-13 11:55
  • 阅读 ( 181 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除