Reputation: 125
I am using regular expression in adobe javascript to find a string of words in a drawing title block. Starting with a number (1) then a date then a varying number of words and 4 sets of initials
var re = new RegExp(1+"\\s\\d{1,2}\\.\\d{1,2}\\.\\d{2,4}\\s\\w+(?:\\s+\\w+){1,9}\\s([A-Z]{2,7})\\s([A-Z]{2,7})\\s([A-Z]{2,7})\\s([A-Z]{2,7})");
The drawing title block example has the following:
1 20.09.16 CHANGES FOR THIS TESTING SB SB BW CR
0 29.07.16 APPROVED FOR CONSTRUCTION MM SB BW GM
The regular expression result is
FOR CONSTRUCTION MM SB BW GM 1 20.09.16 CHANGES FOR THIS TESTING SB SB
I need the regular expression to be
1 20.09.16 CHAINAGES FOR THIS TEST SB SB BW CR
Can anyone advise how to find the exact match starting with the 1 and not random text as shown in the result.
Many thanks for any assistance.
Note: modified regex below works
var re = new RegExp(/^1\s\d{1,2}\.\d{1,2}\.\d{2,4}\s\w+(?:\s+\w+){1,10}\s([A-Z]{2,2})\s([A-Z]{2,2})\s([A-Z]{2,2})\s([A-Z]{2,2})$/g);
I need to replace the 1 at the beginning of the regex from text to a variable.
The regex starts with ^ first then the javascript variable then the regex
var re = new RegExp("//^"+firstWord+"\\s\\d{1,2}\\.\\d{1,2}\\.\\d{2,4}\\s\\w+(?:\\s+\\w+){1,9}\\s([A-Z]{2,4})\\s([A-Z]{2,4})\\s([A-Z]{2,4})\\s([A-Z]{2,4})$//g");
The regex is not working, can anyone advise how to combine java variable with regex?
Upvotes: 0
Views: 213
Reputation: 10434
I'm not sure why your result turned out like that, but if you just do
var str = '1 20.09.16 CHANGES FOR THIS TESTING SB SB BW CR 0 29.07.16 APPROVED FOR CONSTRUCTION MM SB BW GM'
var result = str.match(/1\s\d{1,2}\.\d{1,2}\.\d{2,4}\s\w+(?:\s+\w+){1,9}\s([A-Z]{2,7})\s([A-Z]{2,7})\s([A-Z]{2,7})\s([A-Z]{2,7})/g)
This return
[ '1 20.09.16 CHANGES FOR THIS TESTING SB SB BW CR' ]
To get the string, you just need to do result[0]
Upvotes: 1