即mys字符串:法国(法兰西共和国) 我想只获得法国并删除参数中的字符串。
France (Republic of France)
怎么做? 感谢。
答案 0 :(得分:0)
您可以通过空格split
此字符串,然后获取结果数组的第一个元素(它将只是第一个单词):
const str = "France (Republic of France)";
const stringFinal = str.split(" ")[0];
console.log(stringFinal);
答案 1 :(得分:0)
我认为,此代码可以帮助您。
const str = "France (Republic of France)";
const str2 = "Costa Rica (country Costa Rica)";
const str3 = "United States of America";
function getCountryNameFromStr(str) {
var pattern = /(.*?)( \(.*?\))/gi;
var match = pattern.exec(str);
return match !== null ? match[1] : str;
}
console.log(getCountryNameFromStr(str));
console.log(getCountryNameFromStr(str2));
console.log(getCountryNameFromStr(str3));