I have a regex that replaces the link from the moment http to the occurrence of certain characters in []
, I would like to add
to these characters - that is to replace the string with the occurrence of certain characters or a hard space:
"https://test.pl'".replace(/https?:\/\/[^ "'><] /g," ")
Works fine for the characters mentioned in []
, I don't know how to add here  
;
CodePudding user response:
You can use
.replace(/https?:\/\/.*?(?: |[ '"><]|$)/g," ")
See the regex demo.
Details:
https?:\/\/
-http://
orhttps://
.*?
- any zero or more chars other than line break chars as few as possible(?: |[ '"><]|$)
- one of the following:
-
char sequence|
- or[ "'><]
- a space,"
,'
,>
or<
char|
- or$
- end of string.
JavaScript demo:
const texts = ["https://test.pl test","https://test.pl'test"];
const re = /https?:\/\/.*?(?: |[ '"><]|$)/g;
for (const s of texts) {
console.log(s, '=>', s.replace(re, " "));
}
CodePudding user response:
.replace(/https?:\/\/(?: |[^ "'><]) /g," ")
just replace your code with this one.