1

Well,

I've a JavaScript variable like below.

 var myVar = '<p>This is ~ro..<strong>Yes it is ~hon no?</strong>. happy halloween ~yoo. <strong>~soon</strong> Ho ~no2 ~yes Ho Ho...~jik</p>';

I want to get all words after tilde (~) in a JavaScript array using regular expression or any other method. In the above case, the array result will be something like below.

["ro","hon","yoo","soon","no","yes","jik"]

The words can be of any length. Ultimately I want to get all words after ~ in a JavaScript array variable.

3 Answers 3

3
~([a-zA-Z]+)

try this.grab the captures.See demo.

http://regex101.com/r/yG7zB9/15

var re = /~([a-zA-Z]+)/gm;
var str = '<p>This is ~ro..<strong>Yes it is ~hon no?</strong>. happy halloween ~yoo. <strong>~soon</strong> Ho ~no2 ~yes Ho Ho...~jik</p>';
var m;

while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use the regex

~([a-zA-Z]+)

see how the regex matches http://regex101.com/r/uL5eX0/1

1 Comment

This will include ~ as well which OP does not want
2

like this:

_list = myVar.match(/~(\w+)/g)

var myList = []

for( i in l){myList.push(l[i].split("~")[1])}

console.info(myList)

=======

update: change regEx to:(thanks for @nu11p01n73R)

_list = myVar.match(/~([a-zA-Z]+)/g)

2 Comments

small problem with the solution. you cannot use \w as the example contains ~no2 of which no2 will be matched with \w, op wants just no
Yer,thanks for @nu11p01n73R 's notice.I really ingore this error.And have update it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.