2016-11-26 80 views
1

好了,所以我有以下字符串,替换字符串中的一些

var str = 'some text {Foo} some more text {9}'; 

现在我知道如何更换特定的字符/词一样,

var newStr = str.replace(/{Foo}/g, 'bar'); 

但我怎么更换{ 9}号码可以是任何数字?

+6

'=中newstr str.replace(/ {\ d +} /克, '东西');' – anubhava

+1

[特殊字符正则表达式中的意思(HTTPS://developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Special_characters_meaning_in_regular_expressions) – Andreas

+1

@anubhava谢谢:) – VilleKoo

回答

4

您可以使用元序列\d

var str = 'some text {Foo} some more text {9}', 
 
    newStr = str.replace(/{\d+}/g, 'bar'); 
 

 
console.log(newStr);

+1

谢谢!这正是我一直在寻找的:) – VilleKoo

1

应使用\{/d+}/g正则表达式匹配任意数目:

  • \d元字符相匹配的数字
  • +量词指1个或多个匹配的类型的
  • The /g修饰符意味着执行全局匹配

var str = 'some text {Foo} some more text {9}'; 
 
var newstr = str.replace(/{\d+}/g, 'number'); 
 
console.log(newstr); 
 
str = 'some text {Foo} some more text {99199} and {435}'; 
 
newstr = str.replace(/\d+/g, 'number'); 
 
console.log(newstr);

顺便说一句,我用this tool测试我的正则表达式,认为这可能会派上用场,如果你想尝试更多的正则表达式。

1

您没有指定什么是“数字”,所以我设计了一个解决方案,该解决方案不仅适用于正整数,而且适用于有符号整数和浮点数,并处理嵌套括号。

/{\d+}/g匹配大括号

var str = 'some text {Foo} some more text {9}'; 
 

 
var m = str.match(/{\d+}/g); 
 
console.log(m); // m = [ "{9}" ]

内的一个或多个数字,但上面的图案不匹配浮点数,例如{3.14}

str = 'some text {Foo} some more text {9} and {3.14}'; 
 

 
m = str.match(/{\d+}/g); 
 
console.log(m); // m = [ "{9}" ]

又是怎么回事双括号是,他们还好吗?

str = 'some text {Foo} some more text {9} and {3.14} and {{8}}'; 
 
m = str.match(/{\d+}/g); 
 
console.log(m); // m = [ "{9}", "{8}" ]

您可以使用此匹配花车

str = 'some text {Foo} some more text {9} and {3.14} and {{8}}'; 
 
m = str.match(/{(\-|\+)?(\d+(\.\d+)?)}/g); 
 
console.log(m); // m = [ "{9}", "{3.14}", "{8}" ]

因此,这里是允许嵌套的大括号

str = 'some text {Foo} some more text {9} and {3.14} and {{8}}'; 
 
var newStr = str.replace(/{(\-|\+)?(\d+(\.\d+)?)}/g, '{NUMBER}'); 
 
console.log(newStr);
解决方案

以嵌套的括号的护理

匹配花括号之间的所有内容,并检查数值(正则表达式为https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

str = 'some text {Foo} some more text {9} and {3.14} and {{8}}'; 
 
m = str.match(/{.+?}/g); 
 
console.log(m); // m = [ "{Foo}", "{9}", "{3.14}", "{{8}" ] 
 
for (var i = 0; i < m.length; i++) { 
 
    mi = m[i]; 
 
    value = mi.slice(1, mi.length - 1); 
 
    // regular expression for parsing floats from: 
 
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat 
 
    if (/^(\-|\+)?([0-9]+(\.[0-9]+)?)$/ 
 
    .test(value)) { 
 
    console.log("matches a number: ", value); 
 
    } 
 
};

写函数只能取代数字值

function replaceNumber(string, repl) { 
 
    return string.replace(/{.+?}/g, function(match) { 
 
    value = match.slice(1, match.length - 1); 
 
    if (/^(\-|\+)?([0-9]+(\.[0-9]+)?)$/ 
 
     .test(value)) { 
 
     return '{' + repl + '}'; 
 
    } else { 
 
     return match; 
 
    } 
 
    }); 
 
} 
 

 

 
str = 'some text {Foo} some more text {9} and {3.14} and {{8}} and {-2}'; 
 

 
newStr = replaceNumber(str, 'NUMBER'); 
 
console.log(newStr); 
 
// newStr = some text {Foo} some more text {NUMBER} and {NUMBER} and {{8}} and {NUMBER}