0

I have this regex that converts any # in a string to a hash tag array

myArr= myStr.match(/#\S+/g);

I also have a this to filter out from a string anything other than alpah numeric minus underscore.

myStr= myStr.replace(/[^a-zA-Z0-9_]/g, "");

But now i want to combine these 2 to make sure that each of the items in that array contain only alpha numeric values including underscore and the # in the begining.

Can anyone help out please, thank you.

1
  • You can't since it is two different operations. Commented Dec 14, 2013 at 21:14

1 Answer 1

1

Why so complicated?

myArr = myStr.match(/#[a-z0-9_]+/gi);

EDIT: I misunderstood your intention. Here's a slightly less simple, but altogether more efficient, solution:

myArr = myStr.replace(/#\S+/g,function(m) {
    return "#"+m.replace(/[^a-z0-9_]+/gi,"");
}).match(/#\S+/g);
Sign up to request clarification or add additional context in comments.

9 Comments

@NoahR Uh, no. It's intentional.
thank you, i took off the i. when i enter #hello_wor-ld expecting #hello_world, i get #h
That's because I missed the +, sorry!
so i put the g back in and #hello_wor-ld produces ["#hello_wor"]
@Niet the Dark Absol I was just testing you ;)
|

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.