Regex for picking a Value After “=”

98
May 31, 2019, at 07:40 AM

How i can select RQR-1BN6Q360090-0001 (without quotes) using Regex in below -

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/gaits/CreateReport.aspx?RptNum=RQR-1BN6Q360090-0001">here</a>.</h2>
</body></html>
I tried this but it does  not work
RptNum=([A-Za-z]+)$
Answer 1

You may use

/RptNum=([\w-]+)/

The pattern will match RptNum= and then capture 1 or more occurrences of word chars (letters, digits and _) or hyphens. See the regex demo and the regex graph:

Note that

/RptNum=([A-Z0-9-]+)/

might be a more restrictive pattern that should work, too. It does not match _ and lowercase letters.

In JS, use it with String#match() and grab the second array item upon a match:

var s = 'Object moved to <a href="/gaits/CreateReport.aspx?RptNum=RQR-1BN6Q360090-0001">here</a>'; 
var m = s.match(/RptNum=([\w-]+)/); 
if (m) { 
  console.log(m[1]); 
}

Answer 2

Here, we can also use an expression that collects the new lines, such as:

[\s\S]*RptNum=(.+?)"[\s\S]*
[\w\W]*RptNum=(.+?)"[\w\W]*
[\d\D]*RptNum=(.+?)"[\d\D]*

and our desired output is saved in (.+?).

Test

const regex = /[\s\S]*RptNum=(.+?)"[\s\S]*/gm; 
const str = `<html><head><title>Object moved</title></head><body> 
<h2>Object moved to <a href="/gaits/CreateReport.aspx?RptNum=RQR-1BN6Q360090-0001">here</a>.</h2> 
</body></html>`; 
const subst = `$1`; 
 
// The substituted value will be contained in the result variable 
const result = str.replace(regex, subst); 
 
console.log(result);

Demo

RegEx

If this expression wasn't desired, it can be modified/changed in regex101.com.

RegEx Circuit

jex.im visualizes regular expressions:

Answer 3

const text = 'RptNum=RQR-1BN6Q360090-0001'; 
 
console.log(text.match(/RptNum=.*/).map(m => m.match(/RptNum=.*/)[0])[0].split('RptNum=')[1]);

I suppose that works

READ ALSO
Set value of field in Firestore document only if the field hasn&#39;t already been set

Set value of field in Firestore document only if the field hasn't already been set

I have a collection whose documents look something like this:

100
Handling valid modal dismissals and true exceptions

Handling valid modal dismissals and true exceptions

In most Angular libraries (and specifically ng-boostrap) there is a way to open modals on the page that block the user from doing anything until they every "close" the modal or "dismiss" the modalIn ng-bootstrap it looks like this

111
Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script

Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script

I have a large data file that I want to be separated/filtered out into separate tabs on a new sheetThey need to be filtered by a certain column containing "BGT" within the string

91
Can&#39;t get scientist.js to run error handler

Can't get scientist.js to run error handler

I am learning how to use scientistOverall, it seems to work fine, but I can't get the error-handling capability to work

107