Is it possible, using jQuery (Or anything else), to remove certain bits of text from an element but leave the rest intact?
I’m using a WordPress plugin that compiles all my tweets into WP posts but the post titles are automatically saved using the full text body of the tweet. For example;
@username http://t.co/XXXXXXXX #hashtag
I want to be able to remove the hyperlink and also the #hashtag
The hashtag will always be the same (Ie; it will always be #hashtag
), but the hyperlink will change with every post. Is there a way that I can do this?
Things could be harder or easier depending on whether the username, link and hashtag are always in the same position of the tweet or not. But I might suggest splitting the tweet string on ‘ ‘ and looping to construct a string without words that begin with ‘@’, ‘http://t.co‘, and ‘#’.
It would be easier with a full example because you may not want to remove all the handles, usernames, and hashtags. But I do suspect there is some uniformity in the format you may want to exploit.
Example:
You can use regular expressions to remove the url and the hastags, for example:
To remove the http part:
The regular expression means
http://
followed by any number of characters until a space (use non-eager, i.e. +?, meaning it will stop at the first space)To remove the hashtag
Here it’s a # followed by any character until a space or end of string
Here’s a complete example.
Good reference for javascript regular expressions.
Jquery Link:
You can do something like this, to get the string without hashtag.You’ll have to get the whole text as string into a variable first.