/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How to separate word by regexp

Hi,
I’d like to get three words from string. They are isolated with begin and end of line and chars ‘:’ and ‘&’.

1) one:two&three
2) one&two:three

Now I’d like to get the word between:
1) ^..: or :..$
2) :..&, &..:
3) ^..& or &..$

there can be any word instead the one, two, three

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@crazy_boyauthorAug 16.2010 — [CODE]
lastWord = null;
wordVal = "one&two:three";
wordVal = "one:two&three";
word = wordVal.match(/(w*)&(w*)/);
console.log(word);
lastWordOnBegin = wordVal.match(/^(w*):.*/);
if (lastWordOnBegin) lastWord = {'where':"^",
'value':lastWordOnBegin[1] }
else {
lastWordOnEnd = wordVal.match(/.*:(w*)$/);
if (lastWordOnEnd) lastWord = {'where':"$",
'value':lastWordOnEnd[1] }

}
if (lastWord.where=="^") console.log(lastWord.value+" on begin");
else if (lastWord.where=="$") console.log(lastWord.value+" on the end");
else console.log("Last word was not found");
[/CODE]
×

Success!

Help @crazy_boy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...