[英]Return string between square brackets and slashes inside
How to return string/values between square brackets and slashes inside like : 如何在方括号和斜杠之间返回字符串/值,如:
var valueX = "[/This is Value/]"
After catch, I need result : This is Value. 在捕获之后,我需要结果:这是价值。
Thanks for your help. 谢谢你的帮助。
Using replace
: 使用
replace
:
'[/This is value/]'.replace(/\[\/(.*?)\/\]/, '$1'); // "This is value"
Use the global flag ( //g
) to replace all occurences : 使用全局标志(
//g
)替换所有出现的事件:
'[/a/] [/b/] [/c/] [//]'.replace(/\[\/(.*?)\/\]/g, '$1'); // "a b c "
Use a Regular Expression : 使用正则表达式 :
var valueX = "[/This is value/]";
valueX.replace(/^\[\/(.*)\/\]$/, '$1');
Breaking it down, ^
matches the start of the line. 将其分解,
^
匹配行的开头。 \\[\\/
matches the initial [/
; \\[\\/
匹配最初的[/
; the backslashes are to stop them being interpreted as special characters. 反斜杠是为了阻止它们被解释为特殊字符。
(.*)
means match zero or more *
of any character .
(.*)
表示匹配任何字符的零或更多*
.
and save it as a group ()
. 并将其保存为组
()
。 \\/\\]
is the final /]
, and $
matches the end of the line. \\/\\]
是最终的/]
, $
匹配行的结尾。 The $1
in the replacement string tells it to use the first matched group, in our case the zero or more of any character. 替换字符串中的
$1
告诉它使用第一个匹配的组,在我们的例子中是任何字符的零个或多个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.