prasad prasad - 3 months ago 12
HTML Question

Get the string using regex after the matching words?

I have one string :



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

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





I Need ah whole string after the
src=
string match.i need a answer like
www.123@gmail.com(testing)
.please help me ...correct my code

Answer

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?src=www.123@gmail.com(testing)');

Check the explanation of this regular expression and test on different strings at https://regex101.com/r/zW1lK8/1

Comments