Use Regular Expression to Match any string not starting with + but allow +1 - Javascript RegExp

Javascript examples for RegExp:RegExp Match

Description

Use Regular Expression to Match any string not starting with + but allow +1

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
console.log(/^(\+1|[^+]|$)/.test(""));      // true
console.log(/^(\+1|[^+]|$)/.test("abc"));   // true
console.log(/^(\+1|[^+]|$)/.test("+1"));    // true
console.log(/^(\+1|[^+]|$)/.test("+1abc")); // true
console.log(/^(\+1|[^+]|$)/.test("+2"));    // false
console.log(/^(\+1|[^+]|$)/.test("+abc"));  // false

      </script> 
   </head> 
   <body>  
   </body>/*from  w w w . j  a v  a2s . c  o m*/
</html>

Related Tutorials