<?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>
Output
this is. . test. string.
I want it in "this is a. test. string."
var res = str.replace(/ \. /g, ". ");- you have to quote the.character, and it's easier to use regex literals.var str = <?php echo json_encode($str); ?>;to avoid special characters from spilling over into your JavaScript.