使用JQuery拆分-遍历字符串 [英] Split using JQuery - Loop through the String

查看:779
本文介绍了使用JQuery拆分-遍历字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串如下

var str = "foobar~~some example text~~this is a string, foobar1~~some example 
text1~~this is a string1";

我需要遍历该字符串并获取文本"some example text","some example text1"

I need to loop through this string and get the text "some example text", "some example text1"

任何人都可以让我知道如何循环进行此操作.

Can any one let me know how to loop through this.

推荐答案

您可以使用

You may use .match() with the following regex:

/~~[^~]+~~/g

为了循环,您可以使用 .forEach()在结果数组上:

In order to loop you may use .forEach() on resulting array:

reatVal.forEach(function(ele, idx) {
    console.log('element n.: ' + idx + ' value: ' + ele)
})

var str = "foobar~~some example text~~this is a string, foobar1~~some example text1~~this is a string1";
var retVal = str.match(/~~[^~]+~~/g).map(function(ele, idx) {
    return ele.replace(/~~/g, '');
});


console.log('retVal is the following array: ' + retVal);

retVal.forEach(function(ele, idx) {
    console.log('element n.: ' + idx + ' value: ' + ele)
})

这篇关于使用JQuery拆分-遍历字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆