0

Im trying to match lines that have this format. Writing a static regEx is fine but I need to do this using 2 variables to create the regex dynamically.

I cant seem to get how to escape the forward brackets properly ive tried escaping them, not escaping them and even double escaping them (just for the heck of it) but fireBug shows the actual regEx being created the same no mater how I do itand it doesnt match my input.

Lines to match look like this: 9.SSRDOCSWSHK1/////23NOV96/M//YEUNG/WINSTON/JEREMY-5.1

What Ive tried:

var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[/]{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");

FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g

---------------------------

var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\\d\\d[A-Z]{3}\\d\\d\/[MF]\/\/'+curGstNme+'([\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");

FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g

---------------------------

var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\\/]{5}\\d\\d[A-Z]{3}\\d\\d\\/[MF]\\/\\/'+curGstNme+'([\\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");

FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g

3 Answers 3

1

In regex you need to escape the DOTs since DOT will mean any character.

Use this regex:

regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+ curGstNme + '([/A-Z]+)?-' + pax.slice(0,1) + '\\.';
Sign up to request clarification or add additional context in comments.

1 Comment

nice catch, I missed that one
0

Actually the problem was elsewhere. Apparently fireBug just shows the created regex (in the "watch" panel) with the escaping sashes visible. This made me think the regex was not being created correctly.

Comments

0

Your regex could be reduced like this:

var regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d{2}[A-Z]{3}\\d{2}/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'

What did I change ?

  • \\d\\d can be replaced efficiently with \d{2}.
  • . replaced with \\. for matching exactly dot.
  • [/]{5} replaced by /{5}

This visually gives you:

Regular expression visualization

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.