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

如何使用JavaScript从字符串中提取末尾的特定文本片段

提取字符串最后一段的实现方法

核心思路是以横杠-为分隔符拆分字符串,取最后一个元素,或者用正则匹配最后一个横杠后的内容,以下是几种常用编程语言的实现方式:

Python

s = "this-is-the-whole-string-value-33297af405e6"
result = s.split("-")[-1]
print(result)  # 输出: 33297af405e6

也可以用正则表达式匹配:

import re
s = "this-is-the-whole-string-value-33297af405e6"
result = re.search(r"[^-]+$", s).group()
print(result)

JavaScript

const s = "this-is-the-whole-string-value-33297af405e6";
const result = s.split("-").pop();
console.log(result); // 输出: 33297af405e6

Bash(Shell脚本)

s="this-is-the-whole-string-value-33297af405e6"
result=${s##*-}
echo $result # 输出: 33297af405e6

内容的提问来源于stack exchange,提问作者Mohammed Bekele

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:40:01