简体   繁体   English

删除 NodeJS 字符串中的括号

[英]Remove brackets in NodeJS String

I have a variable in Nodejs with square baskets.我在 Nodejs 中有一个带有方形篮子的变量。 I need to remove the square brackets and extract the data out of it我需要删除方括号并从中提取数据

let value = "[dfsdf][dsfsd][sdfs]MY VALUE";

I need MY VALUE from the value variable.我需要来自 value 变量的 MY VALUE。 the number of square brackets is not final.方括号的数量不是最终的。 Sometimes it will be 1 or it can go up to 4有时它会是 1 或者它可以 go 最多 4

a simple solution without regex will be一个没有正则表达式的简单解决方案将是

value.split(']').slice(-1)[0]

or as per @Jeremy's comment.或根据@Jeremy 的评论。

.split("]").pop()

Another simple solution would be:另一个简单的解决方案是:

let value = "[dfsdf][dsfsd][sdfs]MY VALUE";
let myValue = value.substr(value.lastIndexOf(']')+1)

Use this regular expression to match square brackets:使用这个正则表达式来匹配方括号:

/[\[\]]+/g

 let value = "[dfsdf][dsfsd][sdfs]MY VALUE"; console.log(value.replace(/[\[\]]+/g, ''))


Should you need to replace it with space use如果您需要用空间使用来替换它

value.replace(/[\[\]]+/g, ' ')

 let value = "[dfsdf][dsfsd][sdfs]MY VALUE"; console.log(value.replace(/[\[\]]+/g, ' '))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM