I’m working with strings. The string often contains something like this:
"The site https://example.com contains all the information you need"
So far I have this regex, which splits the string when https? is found
const rgx_link = /(?=https?://)/gi;
So in this case it would split the string into:
arr = ["The site ", "https://example.com contains all the information you need"];
I would like to modify the regex, so it also splits the string if a space is found after the url.
The desired result would look like this:
arr = ["The site ", "https://example.com", " contains all the information you need"];
The new regex "should look" something like this ((?=https?://)(s))
, but it doesn’t work.
Any help would be greatly appreciated. Thank you.
const text = "The site https://example.com contains all the information you need";
const rgx_link = /(?=https?://)/gi;
const result = text.split(rgx_link);
console.log(result)
Edit 1: Wiktors suggestion is correct. Didn’t notice that the leading part of the regex was changed, so it didn’t work.
const text = "The site https://example.com contains all the information you need";
const rgx_link = /(https?://S*)/gi;
const result = text.split(rgx_link);
console.log(result)
3
Answers
Is this what you need?
You can use
Note that
String#split
method outputs all captured substrings when the regex contains capturing groups, so this regex, being entirely wrapped with a capturing group, basically tokenizes the string into URLs and non-URL tokens.Note the absence of the
g
flag, sinceString#split
behavior by default is to split on all occurrences of the regex pattern.Pattern details
http
– ahttp
strings?
– an optionals
char://
– a://
substringS*
– zero or more non-whitespace chars.in python I can search for an url then use the span position and use the end value to split the string after the url