[英]How to get the substring from between two [ ] braces in a string in javascript or jquery
我的字符串看起来像这street no [12]
我需要从字符串中单独提取12
。
如何在 javascript 或 jquery 中的两个[
]
之间获取 substring?
在第一个匹配的 [] 括号之间找到 substring
我注意到您的要求在上一个答案下方的评论中发生了变化,要解决该问题,您可以更改正则表达式添加?
因此它将捕获尽可能少的匹配项。
const myString = "street no [12][22]" const match = myString.match(/\[(.+?)\]/); const myNumber = match && match[1]; console.log(myNumber);
或者,如果这更适合您的需求,您可以直接捕获数字
const myString = "street no [12][22]" const match = myString.match(/\[(\d+)\]/); const myNumber = match && match[1]; console.log(myNumber);
您可以为此使用正则表达式:
const myString = "street no [12]" const match = myString.match(/\[(.+)\]/); const myNumber = match && match[1]; console.log(myNumber);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.