Scanning a string means keeping track of and advancing a position (a zero-based index into the source string) and matching regular expressions against the portion of the source string after the position.
$ npm install strscan $ node-repl > var StringScanner = require("strscan").StringScanner > var s = new StringScanner("This is a test") > s.scan(/\w+/) # => "This" > s.scan(/\w+/) # => null > s.scan(/\s+/) # => " " > s.scan(/\s+/) # => null > s.scan(/\w+/) # => "is" > s.hasTerminated() # => false > s.scan(/\s+/) # => " " > s.scan(/(\w+)\s+(\w+)/) # => "a test" > s.getMatch() # => "a test" > s.getCapture(0) # => "a" > s.getCapture(1) # => "test" > s.hasTerminated() # => true
Copyright (c) 2010 Sam Stephenson. Distributed under the terms of an MIT-style license. See LICENSE for details.