Reputation:
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.
Upvotes: 1
Views: 385
Reputation: 23500
Again, given the criteria in the comment this works
var arr = str.match(/(?:#([^_]*)_|([^#_\s])+)/g)
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 oneUpvotes: 4
Reputation: 198324
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);
Upvotes: 1
Reputation: 119837
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.
Upvotes: 3