prasad prasad - 8 months ago 31
HTML Question

Get the string using regex after the matching words?

I have one string :

function test(){
var datas = "http://localhost/test/test.html?";
var spl = datas.match(/[^src=]?\b*/g);
document.getElementById('demo').innerHTML = spl;

<p id="demo"></p>

I Need ah whole string after the
string match.i need a answer like
.please help me ...correct my code


You can use regular expression capturing groups and exec() function from JavaScript RegExp prototype.

For example, following code will extract everything after the first occurrence of src= or return null if provided string doesn't match.

function extract(str) {
  var regex = /^(?:.*src=)(.*)$/i;
  return regex.test(str) ? /^(?:.*src=)(.*)$/i.exec(str)[1] : null;

document.getElementById('demo').innerHTML = extract('http://localhost/test/test.html?');

Check the explanation of this regular expression and test on different strings at