<?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.
我希望它在“这是一个.test。字符串。”
答案 0 :(得分:2)
var str = "this is a . test . string.";
var res = str.split(" . ").join(". ");
console.log(res);
答案 1 :(得分:0)
使用RegExp
/\s+(?=\.)/g
匹配后跟.
字符的空格字符
var str = "this is a . test . string."
str = str.replace(/\s+(?=\.)/g, "");
document.body.textContent = str;