JavaScript中的字符串替换无法正常工作


String replace in javascript not working properly

<?php
$str = "this is a . test . string.";
?>
<script>
    var str = '<?php echo $str; ?>';
    wordss = ' . ';
    var res = str.replace(new RegExp(wordss,"g"),". "); //space(.)space convert to (.)space
    console.log(res);
</script>

输出

this is. . test. string.

我想要它在"这是一个测试。字符串。

var str = "this is a . test . string.";
var res = str.split(" . ").join(". ");
console.log(res);

使用 RegExp /'s+(?='.)/g 匹配后跟.字符的空格字符

var str = "this is a . test . string."
str = str.replace(/'s+(?='.)/g, "");
document.body.textContent = str;