我需要删除不同字符串中的文本。
我需要一个函数来执行以下操作...
test: example1
preview: sample2
sneakpeak: model3
view: case4
...看起来像这样:
example1
sample2
model3
case4
我尝试使用 substr
和 substring
函数,但无法找到解决方案。
我使用了 selected.substr(0, selected.indexOf(':'))
但返回给我的只是冒号之前的文本。 selected
是包含文本字符串的变量。
由于字符串的长度不同,因此也无法进行硬编码。有什么建议吗?
最佳答案
使用split功能。 split 将返回一个数组。要删除空格,请使用trim()
var res = "test: example1".split(':')[1].trim();
console.log(res);
关于javascript - 从字符串的一部分中删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46242951/