I want to extract a sub-string from a string using javascript, then replace as set of characters by another character.
I know the prefix and postfix of the string. Between the prefix and postfix is the variable sub-string that I want to extract and replace set of characters on it.
For example, this string represents error URL. I need to extract the URL from it which in this example is: https%3A//revoked.badssl.com/
then replace %3A
with :
The logic is that, the prefix is: about:neterror?e=nssFailure2&u=
and the postfix is &c=UTF*
as I do not care about the rest of the string after the &
character.
I probably need to use Regex. However, I know how to use Regex to compare whether a string matches a specific pattern or not. But I wonder how to use Regex to extract a sub-string?
about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&
f=regular d=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.
%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code
%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE
%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A
EDIT: When I try this script:
var myString="about:neterror?e=nssFailure2&u=https%3A//abcde.somethig/&c=UTF-8&f=regular&d=An%20error%20occurred%20during%20a%20connection%20to%20abcde.somethig.\
%0A%0ACannot%20communicate%20securely%20with%20peer%3A%20no%20common%20encryption%20algorithm%28s%29.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20\
title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A";
console.log(myString.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&c=UTF8.*$/, "$1:$2"));
NOTE: I used \
at the end of the line just to break the string in the editor. But it is not part of the original string.
I get output identical to the input.
NOTE2:
What about if I want to just use & as mark of strat for the postfix? I used console.log(myString.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&.*$/, "$1:$2"));
but it prints: https://abcde.somethig/&c=UTF-8&f=regular
and I want: https://abcde.somethig/