需要用Javascript或jQuery替换[...]之间的数字

时间:2016-03-14 12:14:15

标签: javascript jquery

在字符串中:我需要使用Javascript或jQuery将[ ]之间的数字替换为另一个数字。

最好的方法是什么?

Eamples

"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

gl = self.context().versionFunctions()

答案 1 :(得分:0)

使用.replace

"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"

答案 2 :(得分:0)

使用正则表达式:

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

它搜索/\[(\d+)\]/ 集内的数字(无论长度)。

结合替换你可以做到。

测试它:

https://regex101.com/r/zT5kD0/1

答案 3 :(得分:0)

使用replace方法替换部件

    function replaceStr(string,orgText,replaceText){
      var res = string.replace(orgText, replaceText);
      return res;
    }

console.log(replaceStr("[12345].Checked",'12345','1'));

jsfiddle