Find and replace value in a string via regex - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Find and replace value in a string via 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(){//w w w  .  j a v a  2 s . c o  m
str = "http://mydomanin.org/en/components#children-with-disablities";
var replace = { km:"en", en:"km" };
str = str.replace(/km|en/gi, function(match){
  return replace[match];
});
console.log(str);
    }

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

Related Tutorials