禁止输入

时间:2015-11-12 15:51:05

标签: javascript regex csv

我想知道是否可以创建一个允许数字,字母和空格但没有标点符号的正则表达式。

发生的事情是我有一个在线贷款申请,并且在街道地址字段中,一些用户在他们的输入中添加逗号(,)。将应用程序导出为CSV后,由于地址字段中包含逗号,CSV文件的逗号位置错误。

我现在已经玩了几个小时的正则表达式并且它很顺利,但它不能完美。我偶尔会得到一个无法验证但没有标点符号的地址。否则它将是一个可接受的输入,但正则表达式不允许它被验证。

因此,我没有试图规定 允许的内容(数字,空格,字母),我是否可以不仅仅规定允许的内容(标点符号)?后者比前者更有意义。

出于提供信息的目的,我已经提供了我用来测试我的正则表达式的已提交地址列表。我还包括了我一直在研究的正则表达式。非常感谢任何指导或帮助

注意:我添加了无效输入的评论。在无效输入下方,我已经放置了一个现在有效的输入的编辑副本。

地址:

3 TINDERWOOD CRESCENT



117HEIDERAND FLATS EESSENTHOUT STREET HEIDERAND 6511 -- no space after number

117 HEIDERAND FLATS EESSENTHOUT STREET HEIDERAND 6511



3289 ext5 edenpark

120411 mpande loation

1433b Moulton Avenue

1433 b Moulton Avenue

4 diesel rd

10821 Morubisi Street



Unit 44 Charis Place, Prosperity Road, Groblerspark -- punctuation

Unit 44 Charis Place Prosperity Road Groblerspark -- starts with word

44 Charis Place Prosperity Road Groblerspark



p box 3581      -- invalid street address



82 Akasia laan

987 leruleng sectionsaulspoort

1 lenton drive

1179 gugulethu street

1 lenton drive



10269verdwaal2  -- no spaces

10269 verdwaal 2



15 Prinsloo Street

1179 gugulethu street



13 Adler str, Eden Park -- has punctuation

13 Adler str Eden Park



410 wonderzicht, 538 de beerstr -- has punctuation

410 wonderzicht 538 de beerstr



1551        -- invalid street address



52 Koedoe Ave

26a high road orchards



Musina ext9 4363    -- number in wrong place - See below

4363 Musina ext9



18 Replubliek street

753 steve biko avenue unit2

221 Buitekant straat

54 Zone A

B1287 UMSOLWA ROAD

54 Zone A

6574 LETLHODI STREET UNIT 14

27 k street

9635 Zanemvula street

2667 nthatisi Street

498 CESSNA AVENUE



maromene 17         -- number in incorrect place - see below

17 maromene



F1 Ngoje cres MarainnhillM

6574 letlhodi street unit 14

21 tecomastreet

21 tecomastreet

07 Frara Drive

6440 MAYANA STREET THEMBALETHU GEORGE 6529



no.87 Vista Villas      -- has word before number and has punctuation

87 Vista Villas



12 jama street z section    --- !!!!!!!!!!!!!!!!!!!!!!!!



B1287 Umsolwa road

B 1287 Umsolwa road     -- space after B



6574 letlhodi street

7658 Itsomo Steet Ext6



5dharrisson str     -- no space fter number

5d harrisson str



322 Lenham Drive, Lenham    -- has punctuation

322 Lenham Drive Lenham



schoolstreet 3      -- name before number

3 schoolstreet



50 Hercules Court

1546 sefatsa stand

61 sixwila street

20800 mamelodi east



colchester crescent



12 Iraq Street

12 moshoeshoe str

21 vansoelen street

12 moshoeshoe str

4102 Geelhout Street



ward 16, umzumbe, hibberdene        -- has punctuation

ward 16 umzumbe hibberdene



839 Maokeng ext



28caledon drive



34 Leo ave



6423 Bandura Street, Willow Manor Ext. 1



8541 Snake Park

7 INSISWA WAY EXT 12 SHERWOOD PARK 7349



plot 50 kareebos



404 Windermere street

404 Windermere street

404 Windermere street

68 Flamink Street

68 Flamink Street

71 Eagle Dawn Zeiss road



block r3 room 105 n2 gateway



50 plein str

B206 Chapters

225 Buitenkant Street

9 Chestnut street Bonteheuwel

1740a Ben Naude Drive Zone 2



Droedam Farm



16 Aberfeldy Road The Hill



11th road 25 simonsig noordwyk



49 Soetdoring



Allensnek


12935


29 likhonda street

6 Matume street



Plot 88



22 Community Road


1100 kwamakhutha township, po amanzimtoti



9 balmoral heights, balmoral road



3 makubalo streets

3951 umthungwa street birch acres ext 23

12986 Walter Sisulu Street

11138 nongoma str

51 rudloff rd



10 Villa Palazzo, Belami Drive



321 Francis Baard Street

65 9 th street

35 Fiskaal street

14 olive street

9 manho street

38 victoria road

354 Ethafeni Section

16 Arkeldien Street



6.5 Boundary Road



100 Vlamboom Road

902 Mquma Street Bophelong

35 amsterdam street

55 fanplam gardens palmview

6213 Zone 12 Extion Sebokeng



stand no.043 ooslope



3 MURISON STREET

177 govan mbeki township

9 Chestnut street Bonteheuwel

4347 BLOCK B

1a roy cambell street

146 poole avenue

116 Woburn Street

545 Phase 5B Buhlepark

33 vos street



37 fifth street , rusthof



18 Marinda Crescent Marinda

16 chromite ave



13 kanti street 35263,Harare



67 Third Street

270 queen elizabeth ave

30 Victoria

9 manho str

26 Liebenhof Flats Young Str

1 Carter Street



10 Villa Palazzo, Belami Drive



11 vlasblomsingel progress



block 44, 513 sunset avenue



1073 mphafa road

1344 Pickerel place

1073 mphafa road



sweatpealaan 2751



44 Brummerstreet

151 Ext 3

433 10 kodi street

617 rondomstraat Louterwater

10 GREEN STREET

149 tamsanqa street

3 Newfeld Street

76 Dorp Street

5 Mabille Street

28 heathbury place

2356 joyce ndinisa road

A13 Lekoane street

正则表达式:

/^(\w*)(\d+)(\w)?\s(\w+(\s?\w+?))+$/

2 个答案:

答案 0 :(得分:1)

为什么你需要一个正则表达式?你可以做一些简单的事情:

 var address = "My address, with commas not allowed";
 var newAddress = address.replace(",","");

正如我在评论中所说,使用此方法,您可以让用户根据需要编写地址,但不会破坏CSV分隔符。您将能够写出这样的地址

  

Street Nowhere N°10 - 2(12000)

它不会破坏您的CSV。使用正则表达式,此地址将为:

  

Street Nowhere N 10 2 12000

这不太可读。

答案 1 :(得分:1)

更好的解决方案是使用逗号来逃避字段。见https://stackoverflow.com/a/769675/227299。您还需要转义任何双引号。

如果您在JavaScript中执行此操作,则可以执行以下操作:



function escapeCsvValue(val) {
    if (val.indexOf(',') > -1 || val.indexOf('"') > -1) {
        return '"' + val.replace(/"/g, '""') + '"';
    }
    return val;
}
var csv = [
    ['Name, CommaForFun', 'Address'], 
    ['Jack','Calle Ocidental, 2'], 
    ['Jill O"quote"','125, The "cool" place'], 
    ['John','123 Main st'], 
];
   
var escapedCsv = csv.map(function(row){
    return row.map(escapeCsvValue).join(',');
});

var csvString = escapedCsv.join('\n');
console.log(csvString);
/*
"Name, CommaForFun",Address
Jack,"Calle Ocidental, 2"
"Jill O""quote""","125, The ""cool"" place"
John,123 Main st
*/