Allow only letters and spaces with string replace and regex - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Allow only letters and spaces with string replace and regex

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from w w  w. j  av  a 2  s .  c  o m
var x = "foo foo".replace(/^[a-z\s]*$/, "XXX");
console.log(x);
var x = "foo|foo".replace(/^[a-z\s]*$/, "XXX");
console.log(x);
var x = "foo   foo".replace(/^[a-z\s]*$/, "XXX");
console.log(x);
var x = "foo 543foo".replace(/^[a-z\s]*$/, "XXX");
console.log(x);
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials