Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - Replace text inside of square brackets

var a = "[i] earned [c] coin for [b] bonus";

How to get string "__ earned __ coin for __ bonus" from the variable above in JavaScript?

All I want to do is to replace all the bracket [] and its content to __.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
a = a.replace(/[.*?]/g, '__');

if you expect newlines to be possible, you can use:

a = a.replace(/[[^]]*?]/g, '__');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...