-1

One regexp function is below:-

$(document).ready(function(){
  $('button').click(function(){
  var str = 'abcdefghijklmnopqrstuvwxyz';
    var spl = str.match(/input/g);//i need string match depent on value of input
  $('#demo').text(spl);
  });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" id="text">
<button>call</button>
<p id="demo"></p>
how to match the value from corresponding input value. eg:

input k out k;
input y out y;

Thanks in advance...

2
  • Could you please clarify? The question and the code are rather unclear. Commented May 6, 2016 at 10:57
  • 1
    Are you asking how to convert the text from input to a regex? Commented May 6, 2016 at 10:59

1 Answer 1

1

You have to get the input value and then use the javascript RegExp object where you can put the input value in the constructor.

Here's what you need :

$(document).ready(function(){
  $('button').click(function(){
    var str = 'abcdefghijklmnopqrstuvwxyz';
    var inputValue = $("#text").val();
    inputValue = inputValue.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    var spl = str.match(new RegExp(inputValue));
    $('#demo').text(spl);
  });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" id="text">
<button>call</button>
<p id="demo"></p>

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

1 Comment

Type * into the input box and see the console text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.