Using Regex to remove spaces between '[' and ']' - Javascript RegExp

Javascript examples for RegExp:RegExp test

Description

Using Regex to remove spaces between '[' and ']'

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 a2s  .  c o m
function replaceMe(string){
    return string.replace(/\[.*?\]/g, function(string) {
        return string.replace(/\s/g, '');
    })
}
console.log(replaceMe("[first name] + [ last name ]"))
console.log(replaceMe("[first name] + last name ]"))
console.log(replaceMe("[first name] + last name "))
    }

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

Related Tutorials