81887

How to extract a variable sub-string from a string [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/901115/how-can-i-get-query-string-values-in-javascript" dir="ltr" rel="nofollow">How can I get query string values in JavaScript?</a> <span class="question-originals-answer-count"> 73 answers </span> </li> </ul>

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.

<strong>NOTE2:</strong> 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/

Answer1:

Simplest I can think of without using some complex regex and assuming the &c and &u are static, is this - first decoding the string as suggested by Jedi

<pre class="snippet-code-js lang-js prettyprint-override">var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=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" var url = decodeURIComponent(str).split("&u=")[1].split("&c")[0]; console.log(url)

And here is why I close as duplicate:

<pre class="snippet-code-js lang-js prettyprint-override">function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=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" var url = getParameterByName("u",str); console.log(url);

Answer2:

What you are trying to do is to retrieve a query parameter from a URL, and then decode it. For this, you should use libraries build for the purpose, not regexp. For example, many regexp approaches will depend on the precise order of the query params, which is something you cannot really (or should not) rely on.

Here's an example using the <a href="https://developer.mozilla.org/en/docs/Web/API/URL" rel="nofollow">URL API</a>, which is stable and <a href="http://caniuse.com/#feat=url" rel="nofollow">widely supported</a> (other than IE11, and a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1377772" rel="nofollow">bug in Firefox</a> for non-http-type URLs such as about:neterror currently in the process of being fixed):

<pre class="snippet-code-js lang-js prettyprint-override">var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=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"; var url = new URL(str); console.log(url.searchParams.get('u'));

Answer3:

You can capture the sub string you need and then use back reference to extract and reformat it:

<pre class="snippet-code-js lang-js prettyprint-override">var s = "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" var url = s.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&c=UTF[\s\S]*$/, "$1:$2") console.log(url)

Using decodeURIComponent:

<pre class="snippet-code-js lang-js prettyprint-override">var s = "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" var url = decodeURIComponent(s).replace(/^about:neterror\?e=nssFailure2&u=(.*)&c=UTF[\s\S]*$/, "$1") console.log(url)

Recommend

  • cURL for Windows can't make a secure connection to the Stack API
  • Cleave.js Phone CA
  • SSLRead() return error -9806/15958)
  • What distributed message queues support millions of queues?
  • Python getting common name from URL using ssl.getpeercert()
  • How to clear text inside text field when radio button is select
  • Alert pop up with LWUIT
  • How to use an array of arrays with array_map(…) in PHP?
  • does jqgrid support a multiple checkbox list for editing
  • Using variable in a value field in jMeter
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • Fetching methods from BroadcastReceiver to update UI
  • C# - Serializing and deserializing static member
  • Problems to linebreak with an int in JLabel
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • jquery mobile loadPage not working
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal