Quick. I have a string: #user 9#I'm alive! and I want to be about to pull out “user 9”.
So far Im doing:
if(variable.match(/\#/g)){
console.log(variable):
}
But the output is still the full line.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
.split(), in order to pull out your desired item..split()turns your string into an array, with the first variable as a separator.Of course, if you just want regex alone, you could do:
This will again give you an array of the items, but a smaller one as it doesn’t use the empty value before the hash as an item. Further, as stated by @Stephen P, you will need to use a capture group(
()) to capture the items you want.