首页 > 解决方案 > 从 JavaScript 中的字符串中删除正斜杠和前面的字符

问题描述

我的应用程序中有各种字符串,例如:

'0//France'
'1//Italy'
'4//Australia'
'1//Asia//Thailand'
'2//EMEA//Greece//Athens'

我想编写一个通用函数/正则表达式,它将上述字符串作为输入(一次一个)并返回以下内容:

France
Italy
Australia
Thailand
Athens

我怎样才能做到这一点?

标签: javascriptecmascript-6

解决方案


正则表达式很容易变得晦涩难懂,所以我建议使用基本String函数:

const a = '2//EMEA//Greece//Athens';
// Find the last occurrence of `/` and return everything after it.
a.slice(a.lastIndexOf('/') + 1);

推荐阅读