我有一个输出地址的变量,例如:Budapest, Mindegy utca, 1002 Hungary。我不需要号码和'Hungary',只是第一部分。
所以如果有逗号跟任何数字,我想分开。
上面地址的输出应该是:Budapest, Mindegy utca
这就是我想做的:
addressVariable.split(', /\[[0-9]+\]/');但它并没有分裂变量。
发布于 2015-01-10 12:29:53
使用String.prototype.replace移除不需要的部分:
'Budapest, Mindegy utca, 1002 Hungary'.replace(/,\s*\d+.*/, '')
// => "Budapest, Mindegy utca"发布于 2015-01-10 12:30:00
只需根据逗号拆分输入,逗号后面跟着零或多个空格和数字,最后打印索引0以获得第一个值。
> "Budapest, Mindegy utca, 1002 Hungary".split(/,(?=\s*\d+)/)[0]
'Budapest, Mindegy utca'或
您可以使用string.match函数。
> "Budapest, Mindegy utca, 1002 Hungary".match(/^.*?(?=,\s*\d+)/)[0]
'Budapest, Mindegy utca'发布于 2015-01-10 13:30:22
若要在逗号后面加上javascript中的任意数字拆分字符串,请使用:
result = text.split(/,(?=\s*\d)/);https://stackoverflow.com/questions/27876229
复制相似问题