Use replace and regex to keep only substring from an input - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Use replace and regex to keep only substring from an input

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <p id="demo">New York, main street, 1.2.3. this is a test</p> 
      <button onclick="myFunction()">replace</button> 
      <script type="text/javascript">
function myFunction() {/* www . j a  v  a2 s  .  c om*/
    var str = document.getElementById("demo").innerHTML;
    var res = str.replace(/,.*/g, "");
    document.getElementById("demo").innerHTML = res;
}

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

Related Tutorials