1

I am using regex to capture hashtags from a string like this:

var words = "#hashed words and some #more #hashed words";
var tagslistarr = words.match(/#\S+/g); 

console.log(tagslistarr);

this will return an array like this;

["#hashed", "#more", "#hashed"]

question: how can i remove the # hash sign from each of the array items? this is what i would like in the end

["hashed", "more", "hashed"]

4 Answers 4

5

Try:

words.match( /#\S+/g ).map( function(x) { return x.replace(/^#/,''); } );

A better way to do this would be zero-width assertions; unfortunately, JavaScript does not support look-behind assertions, only look-ahead. (As an aside, here is a blog post about mimicking lookbehind assertions in JavaScript. I can't say as I care much for any of the solutions, but it's good reading nonetheless. I sure hope lookbehind assertions make it into ES6.)

As pointed out by SimonR in the comments below, Array.prototype.map is not available in IE8 and below. However, it's easy enough to polyfill:

if( !Array.prototype.map ) {
    Array.prototype.map = function(f) { 
        var r = []; 
        for( var i=0; i<this.length; i++ ) r[i] = f(this[i]); 
        return r; 
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Note, array.map is not supported in IE8 or less: MDN
True. Array.prototype.map was introduced in JavaScript 1.6 in Nov 2005. So it's more than seven years old. If you (or your clients) are using a browser that isn't implementing seven year-old features, maybe it's time to switch, hmmm? Also, it's easy enough to polyfill Array.prototype.map.
In principal I completely agree with you, but unfortunately there are still a lot of folk out there in the real world using IE8 (or less!). Good point about the polyfill though, it's one reason I love javascript so much :)
I know...I'm getting increasingly frustrated with the "real world", though. More and more, I'm taking the approach of just serving up a page that says "your browser is hopelessly out of date. please upgrade" for people using ancient browsers. There's a point at which you can't keep catering to people who won't upgrade.... Depends on the website/service you're offering, of course.
4
var words = "#hashed words and some #more #hashed words";
var tagslistarr = words.match(/#\S+/g); 
tagslistarr = tagslistarr.map(function(x) { return x.substr(1); } );
console.log(tagslistarr)

prints

["hashed", "more", "hashed"]

Comments

2

Use a capturing group in your regex:

words.replace(/#(\S+)/g, '$1');

EDIT:

To get them as an array:

var tagslistarr = words.match(/#\S+/g);

for( var i = 0, l = tagslistarr.length; i < l; i++ ) {
  tagslistarr[ i ] = tagslistarr[ i ].substr( 1 );
}

1 Comment

That just removes the hashes without creating the array.
1
var words = "#hashed words and some #more #hashed words";
var regexp = /#(\S+)/g; // Capture the part of the match you want
var results = [];
var match;
while((match = regexp.exec(words))) // Loop through all matches
  results.push(match[1]);           // Add them to results array

console.log(results);

Comments

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.