正则表达式的数组内的数组 [英] Regex for Arrays within an Array

查看:198
本文介绍了正则表达式的数组内的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阵列用这些都是内部数组众多值的数组(调用外部阵列ARR1)。我想验证的内阵列具有其与ARR1元素的格式如下[1]作为示例提供(ARR1的其他元素,即ARR1 [2],ARR1 [3],ARR1 [4]等。是相同格式的

I have an array of arrays (call the outer array arr1) with numerous values which are all inner arrays. I am trying to validate that the inner arrays have the following format for its elements with arr1[1] provided as an example (the other elements of arr1, i.e. arr1[2], arr1[3], arr1[4], etc. are of the same format

arr1[1] = ['item1', 'item2', 'item3', 'item4', 'item5']:

item 1 - "abcdef" (variable number of letters)
item 2 - "abcdef" (variable number of letters)
item 3 - "abcdef" (variable number of letters) OR "abcdef asdf" (variable number of letters separated by one whitespace character)    
item 4 - "12345678" (eight digits)
item 5 - "123 456 7890" (telephone number with 3 digits followed by 3 digits followed by 4 digits with two whitespace characters as shown)

下面是我到目前为止的片段(不知道第二行是如何工作 - 从不同的线程SO得到了它):

Here is a snippet of what I have so far (not sure how the second line works - got it from a different SO thread):

function f(s) {
  var s2 = (""+s).replace(/\D/g, '');
  var m = s2.match(/^(\d{3})(\d{3})(\d{4})$/);
}

在此先感谢的任何援助。

Thanks in advance for any assistance.

推荐答案

您可以使用以下方法:

item 1 - \w+ (variable number of letters)
item 2 - \w+ (variable number of letters)
item 3 - \w+(?:\s\w+)? (variable number of letters) OR 
                         (variable number of letters 
                          separated by one whitespace character)    
item 4 - \d{8} (eight digits)
item 5 - (?:\d{3}\s){2}\d{4} (telephone number with 3 digits 
                              followed by 3 digits followed by 4 
                              digits with two whitespace characters as shown)

编辑::您可以使用 ^ \\ w + \\ S \\ W +,\\ S \\ W +?(?:\\ S \\ W +)?,\\ S \\ D {8} \\ S。(?:\\ D {3} \\ S){2} \\ d {4} $ 来直接验证字符串

You can use ^\w+?,\s\w+?,\s\w+(?:\s\w+)?,\s\d{8},\s(?:\d{3}\s){2}\d{4}$ to directly validate the string.

例如:

function validateStr(str) {
  return (/^\[\s'\w+?',\s'\w+?',\s'\w+(?:\s\w+)?',\s'\d{8}',\s'(?:\d{3}\s){2}\d{4}'\s\]$/).test(str);
}

这篇关于正则表达式的数组内的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆