You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript截取字符串中+号之后的内容

JavaScript提取+号之后的字符串内容

可以通过两种简单方法实现需求:

方法一:使用split分割

利用split('+')将字符串按加号分割为数组,取最后一段再去除前后空格:

const originalStr = 'река Марица - гр. Първомай + Maritsa - 132543 - HPoint-Water level';
const targetStr = originalStr.split('+').pop().trim();
console.log(targetStr); // 输出:Maritsa - 132543 - HPoint-Water level

方法二:使用indexOf定位后截取

先找到加号的位置,从该位置的下一位开始截取字符串,同时兼容字符串中没有加号的情况:

const originalStr = 'река Марица - гр. Първомай + Maritsa - 132543 - HPoint-Water level';
const plusPos = originalStr.indexOf('+');
const targetStr = plusPos !== -1 ? originalStr.slice(plusPos + 1).trim() : originalStr;
console.log(targetStr);

内容的提问来源于stack exchange,提问作者Ben Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 04:20:24