0

How to extract sub-pattern from javascript regex matched pattern.

//string: 
var str = textcircle c360 s270 ar2 bl2px_br2px_ml0_mr0;

//matching conditions:
var ang = c.match( /c(\d+)\s+/g ); //matches c360
var startang = c.match( /s(\d+)\s+/g ); //matches s180
var ar = c.match( /ar(\d+)\s+/g );  //matches ["ar2 "]

In case of ang, i need extract only (\d+) subpattern, the number 360, not full c360.

In case of startang, i need extract only (\d+) subpattern, the number 180, not full s180.

In case of ar, i need extract only (\d+) subpattern, the number 2, not full ar2, for some reason it is returned as an array, not as a string.

1 Answer 1

1

regexpattern.exec(string) returns an array with 0th item the whole match and 1st item the first group. In this scenario you need 1st group

//string: 
var str = 'textcircle c270 s270 ar2 bl2px_br2px_ml0_mr0;'

//matching conditions:
var ang = /c(\d+)\s+/g.exec(str)[1]; //matches 270
var startang = /s(\d+)\s+/g.exec(str)[1]; //matches 270
var ar = /ar(\d+)\s+/g.exec(str)[1]; //matches 2

console.log(ang)
console.log(startang)
console.log(ar)

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

1 Comment

@olga Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer.

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.