1

[allchar] のようなものを正規表現と一致させ、それを引き出して他のコンテンツに置き換えることができるようにしようとしています

これが私が試していることです

var text = "[abc123.;.] this is more content";
var replacewith = "[contentreplacedwith]";

reg = new RegExp("/\[{1}.*?\]{1}/g");
if(reg.test(text))
{

    txt = $('#message').val().replace(text,'[' + replacewith + ']');
    console.log(txt);
}

結果は

    [contentreplacedwith] this is more content
4

1 に答える 1

2
reg = new RegExp("\\[.*?\\]", "g");
x = "[abc123.;.] this is more content";
console.log(x.replace(reg, "[contentreplacedwith]"));

If you use RegExp constructor the there's no need to pass / delimiters and the options like g for global are a second argument.

于 2013-08-20T22:06:08.183 に答える