[英]How to copy a part of string between two symbols into another string in javascript? Help me…
var var1="hi (help) me";
I want to copy the letters between ()
into a new variable var2
. 我想将()
之间的字母复制到新变量var2
。 That is, the word 'help'. 也就是说,“帮助”一词。 Am new in Javascript. 是Java语言的新功能。
you can use substring() and indexOf() to extract the text between parenthesis
. 您可以使用substring()和indexOf()提取parenthesis
之间的文本。
var var2 = var1.substring(var1.indexOf('(')+1, var1.indexOf(')'))
Slice() method can be used to extract or copy a part of a string into a new string leaving the original string as it is. Slice()方法可用于将字符串的一部分提取或复制到新字符串中,而原字符串保持原样。 Here is how you can use it: 使用方法如下:
Code: 码:
var var1="hi (help) me"; var var2 = var1.slice(4,8); console.log(var2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.