Why the JavaScript Regex Isn't Working
Problem:
You're encountering a null result when attempting to match a list of points in JavaScript with a regular expression.
Regex Expression:
var reg = new RegExp("/\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)/g");
Solution:
var reg = /\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)/g;
Errors using RegExp:
Note:
While using RegExp is an option, it's often more convenient to use regex literals.
Extracting Values:
var result, points = []; while((result = reg.exec(polygons)) !== null) { points.push([+result[1], +result[2]]); }
This creates an array of arrays containing the extracted numbers. The unary plus ( ) converts the strings to numbers.
The above is the detailed content of Why Is My JavaScript Regex Returning Null When Matching Points?. For more information, please follow other related articles on the PHP Chinese website!