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
3.3k views
in Technique[技术] by (71.8m points)

regex to get all the values starting with [[ and ending ]]

JavaScript Question I have a string like this:

"Hello my name is [[xxx]] and I love [[yyy]]"

How can I use Regex to give me the array like this: xxx, yyy ?


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

1 Answer

0 votes
by (71.8m points)

The regex like this [[.*?]] used within the replace function will do the job:

var str = 'Hello my name is [[xxx]] and I love [[yyy]]';

var resultArray = [];
str.replace(/[[(.*?)]]/g, function(match,match1){
    // write data to result object
    resultArray.push(match1);
});

// show the result in console
console.log(resultArray);

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

...