<?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;