最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrieve substring between two characters - Stack Overflow

programmeradmin4浏览0评论

I have string like this

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

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 /

I have string like this

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

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/

Share Improve this question edited Feb 16, 2012 at 14:52 sinsedrix 4,7754 gold badges38 silver badges66 bronze badges asked Feb 16, 2012 at 14:32 ExceptionException 8,37924 gold badges87 silver badges141 bronze badges 2
  • 4 What exactly do you want to retrieve? – Šime Vidas Commented Feb 16, 2012 at 14:34
  • note that your string object needs to be terminated with a semi-colon. – Mark Schultheiss Commented Feb 16, 2012 at 14:44
Add a comment  | 

6 Answers 6

Reset to default 9

IF you simply want the FIRST string BETWEEN you can use:

var mys= str.substring(str.indexOf('#')+1,str.indexOf("_"));

this returns: "it itA itB"

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]);
}

Strip the end and beginning.

Edit

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

I don't really get why but this works:

var str = $('#a').text();
var results = [];
$.each(str.split("_"), function(){
    var a = this.toString().split("#");
    if(a.length===2) results.push(a[1]);
});

console.log(results);​

You can use this kind of regex:

str.replace(/\s/g, "").match(/#(.*?)_/g, "$1");

See this fiddle.

one line solution to get the array

var arrStr = str.split(/[#_]/);

I would not recommend using regex here as it can be done more efficiently through other methods.

function extractString(template, initChar, finalChar) {
  let i = 0;
  let data = [];
  do {
    if (template[i] == initChar) {
      for (let j = i + 1; j < template.length; j++) {
        if (template[j] == finalChar) {
          data[data.length] = template.slice(i + 1, j);
          i = j + 1;
          break;
        }
      }
    }
  } 
  while (++i < template.length);
  console.log(data)
  return data;
}

extractString("#adj#, #brown# fox jumps over the lazy #dog#.","#","#");

发布评论

评论列表(0)

  1. 暂无评论