Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
なぜこれが機能しないのですか?
var str = "nd2(31)jnd"; str = str.replace(/[0-9](/,/[0-9]*(/);
すべての数字を前の括弧に置き換えたい例: "2(" with "2 *("
あなたの正規表現は間違っています、これはあなたが望むことをします:
str.replace(/([0-9]+)\(/g, "$1*(");
$1parens の最初の一致を参照し、それに一致()するにはエスケープ\(する必要があります。
$1
()
\(
更新:gグローバル マッチング用に追加
g
2(3(4+5)) => 2*(3*(4+5))
更新:括弧の反対側でも機能させるには、組み合わせて:
str.replace(/(\d+)\(/g, "$1*(").replace(/\)(\d+)/g, ")*$1");