-1

Given a regex how can i split filter a string like this :

"https://theuselessweb.com/ hello https://theuselessweb.com/ hello"

To a string like this :

"hello hello"

Ive tried something like

string.match(regex).join(' ')

But this doesnt reeally work Any solutions?

User123123
  • 485
  • 1
  • 5
  • 13
  • What exactly is the desired outcome? Is your input string a list of words delimited by `' '` and you want to extract words that are not an `url`? – cSharp Apr 21 '22 at 02:38
  • https://stackoverflow.com/a/10398955 Replace substrings matching a URL with the empty string – CertainPerformance Apr 21 '22 at 02:42

2 Answers2

0

Here is one way by splitting the string into words and then filtering out the urls.

const str = "https://theuselessweb.com/ hello https://theuselessweb.com/ hello";

const res = str.split(" ").filter(w => !w.includes("https://")).join(" ");

console.log(res);
PR7
  • 1,524
  • 1
  • 13
  • 24
0

You can split the input by split(' ') and filter out the array using a regex expression you want.

const inputStr = "https://theuselessweb.com/ hello https://theuselessweb.com/ hello";

const inputArr = inputStr.split(' ');
const regex = new RegExp('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}');

const output = inputArr.filter(word => !regex.test(word));
console.log(output);
cSharp
  • 2,884
  • 1
  • 6
  • 23