0

how to set variable value in pattern regx in Jquery.I want to set pattern value dyanamically.Please help me How to set

 <!DOCTYPE html>
    <html>
    <body>

    <script language="javascript">
      function checkABC(str){
    var test="abc";
        var pattern = /'test'/gi
        if(str.match(pattern)){
          alert('matches');
        }else{
          alert('no match');
        }
      }
    </script>
    <input type="button" value="matches" onclick="checkABC('deabcfg')" />

    </body>
    </html>

2 Answers 2

2

Use RegExp constructor

function checkABC(str) {
    var test = "abc";
    var pattern = new RegExp(test, 'gi');
    if (str.match(pattern)) {
        alert('matches');
    } else {
        alert('no match');
    }
}

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Use like this :

var pattern = new RegExp("regex"+test,"g");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.