Character substitution on Jquery

Here an easy way to search and replace each occurrence a group of characters (in this case underscore and minus) with another (in this case a space) on a specified element (in this case each A), using Jquery javascript library.

$("a").each(function() {
   $(this).html($(this).html().replace(/[_-]/g," "));
});

The first argument passed on replace is a regex, if you just pass a single character (like “_” or “-“) only first occurrence for each element.

Real world usage: I use this onto a long page, where a table has long filenames with underscore and minus in place of spaces. This trick allows in my case to show tables nicely, without caring on a mass substitution via server side scripting.

See also:

Tags: , , , , , , , , , ,

3 responses to “Character substitution on Jquery”

  1. Lucas says :

    Nice solution! Very usefull! Congratulation!

  2. roXon says :

    Great! thanks!

  3. sanjeewa says :

    thank you very much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 33 other followers

%d bloggers like this: