正常替換
<script>
var str = "Visit Microsoft!"
console.log(str.replace(/Microsoft/, "W3School"))
</script>
複製程式碼
$是特殊符號
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var text1 = "42$###$7$###$7$###$42";
console.log(text1);
var text2 = text1.replace(/\$###\$/g, ",");
console.log(text1);
console.log(text2);
</script>
</body>
</html>
複製程式碼
vue中的替換
<div class="answer-right" v-html="item.answer"></div>
複製程式碼
對比
<div class="answer-right" v-html="item.answer.replace(/\$###\$/g, ', ')"></div>
複製程式碼