Reputation: 3018
I have a text which looks like below
"Here is your result https://polo.felix.com/stat/content/table-1576073323.16.html Please click on the link to see more.."
Now I just want to extract the text after the link which is
Please click on the link to see more..
Now it might happen that there might not be any text after the link in which case I should get an empty string.
This is what I tried to do
message = "Here is your result https://polo.felix.com/stat/content/table-1576073323.16.html Please click on the link to see more.."
if(message.split('html')[1].trim() !== '') {
do_something()
}
But this is not elegant and if the link ends with something other than html
, this won't work.
Is there any regex
that can get content to the right of a url in a text if present or return empty string?
Upvotes: 0
Views: 962
Reputation: 16008
Give this a go.
var str = 'Here is your result https://polo.felix.com/stat/content/table-1576073323.16.html Please click on the link to see more..'
var expr = /https?:\/\/\S+(.*)/g
var match = expr.exec(str)
console.log(match[1])
Upvotes: 1
Reputation: 22817
You can use the following regex (results in capture group 1):
(?:https?:\/{2}\S+\s)(.*)
s = 'Here is your result https://polo.felix.com/stat/content/table-1576073323.16.html Please click on the link to see more..'
r = /(?:https?:\/{2}\S+\s)(.*)/
m = s.match(r)
if(m) console.log(m[1])
Matches URLs beginning with http://
or https://
up to the next space (inclusive), then captures the remainder of the string into the capture group.
Or you can use the following regex in ECMA2018+ (V8 engine +) - see browser compatibility for lookbehind assertions here:
(?<=https?:\/{2}\S+\s).*
s = 'Here is your result https://polo.felix.com/stat/content/table-1576073323.16.html Please click on the link to see more..'
r = /(?<=https?:\/{2}\S+\s).*/
m = s.match(r)
if(m) console.log(m[0])
Does the same as the previous regex, just uses a positive lookbehind to ensure the URL precedes rather than matching it. The regex match is the remainder of the string after the URL.
Upvotes: 2