Javascript examples for RegExp:RegExp Match
Use javascript regex to match string with and without number
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> window.onload=function(){//from www.j a v a2 s .c om var scores = ["this is a test", "test test @inc score 1000"]; var re = /@inc score(?: (\d+))?/; for (i = 0; i < scores.length; i++) { var inc = scores[i]; if (result = inc.match(re)) { var addition = "+=" + (result[1] === undefined ? '1' : result[1]); console.log(addition); } } } </script> </head> <body> </body> </html>