[英]JavaScript - replace square brackets in a string
I have a simple string and trying to convert [~sample] to @sample . 我有一个简单的字符串,并尝试将[~ pample ]转换为@sample 。 For example: 例如:
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology';
string.replace('[~', '@');
I have tried above solution but it only convert the first one and the ] can not be removed. 我已经尝试了上面的解决方案,但它只转换第一个,并且]无法删除。
now I learnt how to use /g 现在我学会了如何使用/ g
Try this code: 试试这段代码:
string.replace(/(\[~)(\w+)(\])/g, function(match, p1, p2, p3) {
// p1 = [~
// p2 = theme / media / whateverWord
// p3 = ]
return '@' + p2
// Returns @whateverWord
})
In the 1st
group: 在1st
组:
\\[
will select a [
\\[
将选择[
~
will select a ~ ~
会选择〜 In the 2nd
group: 在2nd
组:
\\w
will select any alphanumeric character or an _
\\w
将选择任何字母数字字符或_
+
states that the alphanumeric character must appear at least once, ie there must be at least 1
letter between the [~
and ]
+
表示字母数字字符必须至少出现一次,即[~
和]
之间必须至少有1
字母 In the 3rd
group: 在第3rd
组:
\\]
will select any ]
\\]
会选择任何]
In the function: 在功能中:
match
is not used in the output, but it contains the whole matched substring match
不在输出中使用,但它包含整个匹配的子字符串 p1
contains the [~
p1
包含[~
p2
contains the word between the [~
and ]
, ie theme
or media
p2
包含[~
和]
之间的单词,即theme
或media
p3
contains the ]
p3
包含]
The return
statement return
s an @
, followed by the word between the [~
and ]
return
语句return
一个@
,后跟[~
和]
之间的单词
This will replace all [~
with @
这将取代所有[~
with @
Here is a working example: 这是一个工作示例:
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology. Also, [this tag] [will be kept]' document.body.innerHTML = string.replace(/(\\[~)(\\w+)(\\])/g, function(match, p1, p2, p3) { return '@' + p2 })
Edit: Actually, you can make it simpler: 编辑:实际上,你可以使它更简单:
string.replace(/(\[~)(\w+)(\])/g, '@$2')
Check out the demo below: 看看下面的演示:
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology. Also, [this tag] [will be kept]' document.body.innerHTML = string.replace(/(\\[~)(\\w+)(\\])/g, '@$2')
The $2
contains the contents of the second capture group, and the second capture group contains the text between the [~
and ]
. $2
包含第二个捕获组的内容,第二个捕获组包含[~
和]
之间的文本。 So the output is an @
, followed by the text. 所以输出是@
,后跟文本。
This is simpler and faster than the version above, and takes up less space 这比上面的版本更简单,更快,占用的空间更少
You could use RegExp
/(\\[~)|(\\])/g
你可以使用RegExp
/(\\[~)|(\\])/g
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology'; var res = string.replace(/(\\[~)|(\\])/g, function(match, p1, p2) { if (p1) return "@"; if (p2) return "" }); document.body.textContent = res;
The issue is a bit more complicated than simply nesting .replace
[@
and ]
问题比简单嵌套更复杂.replace
[@
and ]
var string = 'Strategic [~theme] areas with cross [~media] technology [this is fine] ok?'; document.body.innerHTML = string.replace(/\\[~([^\\]]+)\\]/g, '@$1');
The ([^\\]]+)
makes sure to capture any character that is not an ]
but is delimited by [~
and ]
, which is a better solution in any case preventing text like [don't mess with me]
to be... messed. ([^\\]]+)
确保捕获任何不是]
但由[~
和]
分隔的字符,这在任何情况下都是一个更好的解决方案,防止像[don't mess with me]
文字......搞砸了。
"Completely engineer client-based strategic [~theme] areas before cross [~media] technology [with some] other bits.".replace(/\[~([^\]]+)\]/g,"@$1");
You need to qualify the tilde in the search. 您需要在搜索中限定波浪号。 I'm surprised at all the crazy down voting. 我对所有疯狂的投票感到惊讶。 People are trying to be helpful here. 人们试图在这里提供帮助。 If someone has a problem with the post and the answers, it's more helpful to explain WHY you downvote and not just go willy-nilly on the down-votes without explaining yourself. 如果有人对帖子和答案有疑问,那么解释你为什么要进行投票更有帮助,而不仅仅是在没有解释自己的情况下对下来投票不屑一顾。
As others have said, google is a friend. 正如其他人所说,谷歌是朋友。 Try searching through www.regular-expressions.info for more help. 尝试在www.regular-expressions.info上搜索以获得更多帮助。
The code below uses Regexp to find all group of sequences that start with [~ and end with ] . 下面的代码使用Regexp查找以[〜和end with ]开头的所有序列组。 It also captures the word in between. 它还捕获了两者之间的单词。 $1 in the second parameter of replace function references the found word. replace函数的第二个参数$ 1引用找到的单词。
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology' document.body.innerHTML = string.replace(/\\[~(\\w+)\\]/g, '@$1');
I think this is what you are looking for https://jsfiddle.net/DIRTY_SMITH/7oe5kh9L/33/ 我认为这就是你要找的内容https://jsfiddle.net/DIRTY_SMITH/7oe5kh9L/33/
string.replace(/\\[~/g, '@').replace(/\\]/g, '');
var string = 'Completely engineer client-based strategic [~theme] areas before cross [~media] technology';
alert("Starting string:" +string);
var res = string.replace(/\[~/g, '@').replace(/\]/g, '');
alert("Final string: "+res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.