就我而言,我正在测试整行。
/(?!^,)^((^|,)([abc]+|[123]+))+$/.test('a,b,c,1,2,3');
true
否定前瞻不包括初始逗号。
/(?!^,)^((^|,)([abc]+|[123]+))+$/.test(',a,b,c,1,2,3');
false
如果您需要单个组件在验证后进行简单拆分。
我正在验证 PLSS 细分部分和子部分。
// Check for one or more Section Specs consisting of an optional
// subsection followed by an "S" and one or two digits. Multiple
// Section Specs are separated by space or a comma and optional space.
//
// Example: SW/4 SW/4 S1, E/2 S2, N/2 N/2 S12
//
// Valid subsections are -
// (1) [NS][EW]/4\s+[NS][EW]/4 eg. NW/4 SE/4 (40 ac)
// (2) [NSEW]/2\s+[NS][EW]/4 eg. N/2 SE/4 (80 ac)
// (3) [NS]/2\s+[NS]/2 eg. N/2 S/2 (160 ac)
// (4) [EW]/2\s+[EW]/2 eg. E/2 W/2 (160 ac)
// (5) [NS][EW]/4 eg. NE/4 (160 ac)
// (6) [NSEW]/2 eg. E/2 (320 ac)
// (7) 1/1 Shorthand for the full section (640 ac)
//
// Expressions like E/2 N/2 are not valid. Use NE/4 instead.
// Expressions like NW/4 E/2 are not valid. You probably want W/2 NE/4.
var pat = '' +
'(([NS][EW]/4|[NSEW]/2)\\s+)?[NS][EW]/4\\s+' + // (1), (2) & (5)
'|([NS]/2\\s+)?[NS]/2\\s+' + // (3) & part of (6)
'|([EW]/2\\s+)?[EW]/2\\s+' + // (4) & part of (6)
'|1/1\\s+'; // (7)
pat = '(' + pat + ')?' + 'S\\d{1,2}'; // a Section Spec
// Line anchors, join alternatives and negative lookahead to exclude an initial comma
pat = '(?!^,)^((^|,\\s*|\\s+)(' + pat + '))+$';
var re = new RegExp(pat, 'i');
console.log(pat);
(?!^,)^((^|,\s*|\s+)(((([NS][EW]/4|[NSEW]/2)\s+)?[NS][EW]/4\s+|([NS]/2\s+)?[NS]/2\s+|([EW]/2\s+)?[EW]/2\s+|1/1\s+)?S\d{1,2}))+$
验证后,我使用积极的后向分析进行拆分。
var secs = val.split(/(?<=S\d+)(,\s*|\s+)/i);