0

I've a string something like

<dt>Source:</dt>
<dd>
    Emergence: Title; 2005, Vol. 9 Issue 30, p120-203, 12p
</dd>

Now I am a regex to fetch different values for it, i.e. : Volume, issue, date etc so, I fetch entire text using :

var attr = jQuery("dl dt:contains('Source:') ~ dd:eq(0)").text();

And use regex to fetch different values, such as :

To fetch start page I use, following regex:

var regex = new RegExp("p\\d+(?=[-\\s]{1})");

var regexValPS = attr.match(regex);

Return value : p120, expected : 120

Similarly, to fetch Volume info, I use following, regex:

var regexVol = new RegExp("Vol.\\s\\d+");
var regexValVol = attributeVal.match(regexVol);

I get : Vol. 9 , I want : 9

Similarly I am getting issue number with "Issue" text :

var regEx = new RegExp("Issue\\s\\d+");
var regExVal = attributeVal.match(regEx);

I Should get : 30 instead : Issue 30

The problem is I can't use another regex to get the desired value, can't strip/parseInt etc, and the pattern must be able to fetch information in a single regex.

4 Answers 4

1

Use grouping (...) and read its match »

Demo:

var str = "Emergence: Title; 2005, Vol. 9 Issue 30, p120-203, 12p";
var re = /p(\d+)(?=[\-\s])/;
document.writeln(re.exec(str)[1]); // prints: 120
re = /Vol\.\s(\d+)/;
document.writeln(re.exec(str)[1]); // prints: 9

Test it here.

Sign up to request clarification or add additional context in comments.

Comments

1

Toget the desired info using a single regex, you need to take advantage of regex grouping:

var regEx = new RegExp("Issue\\s(\\d+)");
var regExVal = attributeVal.match(regEx)[1];

If you cannot modify the regex, you can maybe parse the resulting number :

var number = "Issue 30".replace(/\D/g, '');

Comments

1

If I understand you correctly, you do not want to do further parsing on the string values returned by the .match() calls, but can accept a different regular expression if it returns the necessary values in one statement.

Your regex needs a capture group () to retrieve the desired numbers, and place them in an array index [] (the first index [0] will hold the entire matched string, and subsequent indices hold the () captured substrings).

Instead of new RegExp() you can use the simpler /pattern/ regex literal in this case, and it is possible to extract the desired value in a single statement for all cases.

var yourString = '<dt>Source:</dt>\
<dd>\
    Emergence: Title; 2005, Vol. 9 Issue 30, p120-203, 12p\
</dd>';

// Match the page, captured in index [1]
yourString.match(/p(\d+)(?=[-\s]{1})/)[1];
// "120"

// Match the Vol captured in index [1]
yourString.match(/Vol\.\s(\d+)/)[1];
// "9"

// Match the issue captured in index [1]
yourString.match(/Issue\s(\d+)/)[1];
// "30"

Here it is on jsfiddle

1 Comment

thanx, you understood me correctly, all I wanted was to be able to parse the values in single Regex. Well, I can't directly use Group capture, but, let me see how I can make use of it... Thnx every1 for resoponses
0

Try this:

var attr = jQuery("dt:contains('Source:') ~ dd:eq(0)").text();
console.log(attr);
console.log(attr.match(/p(\d+)(?=[-\s]{1})/)[1]);
console.log(attr.match(/Vol\.\s(\d+)/)[1]);
console.log(attr.match(/Issue\s(\d+)/)[1]);

For more details: JQUERY REGEX EXAMPLES TO USE WITH .MATCH().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.