Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern memory in javascript

Status
Not open for further replies.

new2jscript

Technical User
Mar 10, 2011
3
US
I am looking to loop through a file containing lines of such:

1345672|1
4833891|9
7921648|2

The intended logic is if x matches the first number (before the pipe) then set $secondnumberval to the second number (after the pipe).

I've some basic experience with perl. With perl this can be accomplished with simple pattern memory/buffering or splitting. I'm new to javascript and have been unsuccessful in finding anything similar. Any help would be greatly appreciated
 
You can use the following regular expression:

var re = /(\d)\|(\d)/
if(re.test(testString)){
app.alert (re.exec(testString))
}

This will show you a dialog with the full match and the 2 substrings (the first digit & the second digit).
 
Thank you so much, Mark.

In regards to your snippet, can I assign the values of the 2 substrings to independent variables that I can use later? In perl pattern memory/reference, the first(\d) I could call with $1 and the second with $2.

$bu = "$1";
$fincode = "$2";

 
The result of the re.exec() will be an array with the matches (full match, then each substring that was matched)

var re = /(\d)\|(\d)/
if(re.test(testString)){
var match = re.exec(testString)
app.alert (match[1]) // First substring
app.alert (match[2]) // Second substring
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top