javascript - 使用字符串

标签 javascript jquery

如果我有一个字符串包含

abc: def - 01:00, ghi - 02:00

这个字符串的任何部分都可以改变,除了字符串中分隔符的顺序,即 :, -, :, -:

如何获取第一个 : 之前的文本并将其粘贴到变量中供以后使用?

另外,我如何获取第一个 - 之间的文本, 并将其粘贴到不同的变量中?

最佳答案

How do I get the text before the first : and stick it into a variable for later use?

var text = 'abc: def - 01:00, ghi - 02:00',
    firstChunk = text.substring(0, text.indexOf(':'));
    // firstChunk is "abc"

And also, how do I get the text between the first - and , and stick that into a different variable?

var secondChunk = text.substring(text.indexOf('-') + 1, text.indexOf(','));
// secondChunk is " 01:00"

字符串函数引用:

关于javascript - 使用字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6344467/

相关文章:

javascript - 如何遍历 v8 中全局对象(库)的所有属性/函数?

javascript - 如何: Javascript make <img> go invisible

具有范围和不同背景颜色的 jQuery slider

javascript - 无法在 JSON 数组中找到项目

javascript - 如何根据其中字符串的长度修改 div 的长度?

javascript - 单击时切换 CSS 样式

javascript - SublimeVideo JavaScript

javascript - 是否可以在主显示器以外的其他显示器上显示网络推送通知?

javascript - for循环字符串每个单词

javascript - 滚动时检测第一个顶部元素