简体   繁体   中英

How to split this string into an array

If I have a string like below and It is not static every time.

 var str = "#a
            b
            c
            _
            ele1
            ele2
            #d
            e
            f
            _
            ele3
            ";

from the above string I want to retrieve an array like below

     arr = [ "#a
               b
               c
              _",
             "ele1",
             "ele2",
             "#d
               e
               f
              _",
             "ele3"
           ]

The criterion is: everything between # and _ as a single item; every line outside those delimiters is a separate item.

How can I do that.Any idea.... Please use this fiddle.

Again, given the criteria in the comment this works

var arr = str.match(/(?:#([^_]*)_|([^#_\s])+)/g) 

http://jsfiddle.net/fhDPj/1/

And to explain the regex

  • #([^_]*)_ - find anything that isn't _ that falls between a # and a _ ( * means even empty strings are captured)
  • ([^#_\\s])+ - find anything that isn't # , _ or whitespace ( + means only non-empty strings are captured)
  • (?: | ) - find either of the above (but non-capturing as the above expressions already capture the strings needed)
  • / /g - global match, to return all matches in the string rather than just the first one

are the whitespaces intentional?

try this instead:

<div id ="a">#abc_ele1ele2#def_ele34</div>​

script:

var str = $('#a').text();
var result = str.match(/(#[^_]*_)|([^#][^\d]*\d{1,})/g)
console.log(result)

EXPLANATION:
string.match()      - returns an array of matches
#[^_]*_             - finds anything that begins with # and ends with _ and with anything but _ in between
[^#][^\d]*\d{1,}    - finds anything with that does NOT start with #, followed by 0 or more non-numeric characters,  and ends with at least one digit

DEMO : check your console

this will still run with all those whitespaces. you MUST be clear with your split rules.

var x = str.match(/(#?[a-z]+[0-9_]+?)/g);

Given the criteria in my comment under the question:

var str = "#a\nb\nc\n_\nfoo\nbar\n#d\ne\nf\n_";
var re = /((?:#[^_]*_)|(?:^.*$))/mg;
var result = str.match(re);
console.log(result);
// [ '#a\nb\nc\n_', 'foo', 'bar', '#d\ne\nf\n_' ]

Regexp explanation: a match is either everything from # to _ - (?:#[^_]*_) - or everything on a single line - (?:^.*$) .

EDIT: due to whitespace... a bit different strategy:

var str = $('#a').text();
var re = /^\s*((?:#[^_]*_)|(?:.*?$))/mg;
var result = [], match;
while ((match = re.exec(str))) {
  result.push(match[1]);
}
console.log(result);​

尝试拆分:

arr = str.split("_");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM