检索两个字符之间的子字符串 [英] Retrieve substring between two characters

查看:68
本文介绍了检索两个字符之间的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串

  var str = "#it
              itA
              itB
              _
             #et
              etA
              etB
             _
             etC
             etD"

如何在#和_之间检索元素。截至目前,我正在用新行拆分文本但无法进行此操作。请帮帮我。请使用这个小提琴 http://jsfiddle.net/h728C/2/

How can I retrieve elements between # and _. As of now I am splitting the text with new line but unable to workout this. Please help me on this. Please use this fiddle http://jsfiddle.net/h728C/2/

推荐答案

我在上发布了一些解决方案fidde 。它使用正则表达式

I've posted some solution in fidde. It uses the Regex

var str = $('#a').text();
var pattern = /#([\s\S]*?)(?=_)/g;
var result = str.match(pattern);
for (var i = 0; i < result.length; i++) {
    if (result[i].length > 1) {
       result[i] = result[i].substring(1, result[i].length);
    }
    alert(result[i]);
}

剥去结束并开始。

编辑

更新了小提琴和代码。现在它剥离了开始并结束 _
您可以使用其中任何一个。无论哪个方便。

I've updated the fiddle and the code. Now it strips the beginning # and ending _. You can use either. Whichever is convenient. ​ ​

这篇关于检索两个字符之间的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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