获取四个括号内的字符串(正则表达式)

时间:2017-01-22 15:14:39

标签: javascript regex

我怎么写这样的东西:

var s = "[hello world](one two)";
var x = s.replace(REGEX, '<p>$1</p><b>$2</b>');

输出:

<p>hello world</p><b>one two</b>

3 个答案:

答案 0 :(得分:2)

console.log( "[hello world](one two)".replace(/\[(.*)\]\((.*)\)/, '<p>$1</p><b>$2</b>'));

答案 1 :(得分:1)

将以下正则表达式与negated character class一起使用。

var s = "[hello world](one two)";
var x = s.replace(/\[([^\]]*)]\(([^)]*)\)/, '<p>$1</p><b>$2</b>');

console.log(x);

Regex explanation here.

答案 2 :(得分:0)

将捕获组与regex.exec(字符串)和this(([。])|((。)))+(或类似于此的东西)一起使用并构建theoputut字符串