Xeger是更富有表现力的JavaScript正则表达式,Xeger发音为"zeeger"。
使用:
var xeger = require('xeger');/* Parsig a URL. Gettig the schema, host, path, ad url params *//* Istead of this */var borigRegex= /(https?)\:\/\/([^\/]+)(.+)\?(.*)/;/* Write this! */var coolRegex = xeger(fuctio (x) { /* schema */ x.group(fuctio (x) { x.literal('http'); x.literal('s', { optioal: true }); }); x.literal('://'); /* host */ x.group(fuctio (x) { x.ot('/', { multiple: true }); }); /* path */ x.group(fuctio (x) { x.ay({ multiple: true }); }); x.literal('?'); /* query params */ x.group(fuctio (x) { x.ay({ multiple: true, optioal: true }); });});var matched = coolRegex.exec('https://www.google.com/search?q=my_search');匹配:
[ 'https://www.google.com/search?q=my_search', 'https', /* schema */ 'www.google.com', /* host */ '/search', /* path */ 'q=my_search', /* query params */ idex: 0, iput: 'https://www.google.com/search?q=my_search' ]
评论