2

Am looking to grab the tags from a form and save them to an array of strings to loop over. I would like the tags to be stripped of white spaces, # signs, and commas.

This is almost working, but tagArraySplit[i].replace(/#/g, ''); does not strip the '#'..

var newTags = $( "#newTags" ).val();
console.log(newTags);
// regex split for one or more commas or spaces
// so that multiple consecutive spaces or a comma+space .. are not empty
var tagArraySplit = newTags.split(/[ ,]+/).filter(Boolean);
var tags = [];
for (i=0; i<tagArraySplit.length; i++)
{
    console.log(tagArraySplit[i]);
    tagArraySplit[i].trim;
    tagArraySplit[i].replace(/#/g, '');
    console.log('trimmed a tag');
    console.log(tagArraySplit[i]);
    tags.push(tagArraySplit[i]);
}
console.log(tags);

1 Answer 1

3

You need to store return value of String#replace method:

tagArraySplit[i] = tagArraySplit[i].replace(/#/g, '');
Sign up to request clarification or add additional context in comments.

2 Comments

@anubhava please could you help me with this stackoverflow.com/questions/23797093/…
Sure let me take a look.

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.