0

Can anybody help me writing a regular expression to replace these characters with a empty string. Character list is given below.

public static char[] delimiters = { ' ', '\r', '\n', '?', '!', ';', '.', ',', '`', ':', '(', ')', '{', '}', '[', ']', '|', '\'', '\\', '~', '=', '@', '>', '<', '&', '%', '-', '/', '#' };
3
  • 2
    No regular expression is needed for this. Commented Mar 22, 2010 at 10:41
  • can u be a little descriptive to solve my scenario? Commented Mar 22, 2010 at 10:44
  • Do you want to remove all occurences of any of these characters from a given string? Commented Mar 22, 2010 at 11:02

5 Answers 5

2
var in = "...";
var out = in.replace(/[ \r\n?!:;\-(){}\[\]\\'"=@><&%\/#]+/g, '');

I may have missed a couple of characters.

An alternative solution may be to take a white list rather than black list approach:

var out = in.replace(/[^\w]+/g, '');

This will remove anything that isn't a word character, meaning a letter (uppercase or lowercase), digit or underscore.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not working. I tried , it can't give the replaced string. anyways thanks for the effort
I want to replace only those characters (strictly) mentioned in the list.
1

Here's the regex you want:

var re = /[ \r\n?!;.,`:(){}\[\]\|\'\\~=@><&%-\/#]/g;

For example:

var test = "j a\nv\ra?s!c;r.i,p`t:I(s)A{w}e[s]o|m'e\\~=@><&%-/#";
test.replace(re, '')

>>> "javascriptIsAwesome"

FYI, the general notation for this sort of thing in regular expressions is "/[...]/" - means, "match any character inside the brackets"

Comments

1

Try this:

var delimiters = [' ', '\r', '\n', '?', '!', ';', '.', ',', '`', ':', '(', ')', '{', '}', '[', ']', '|', '\'', '\\', '~', '=', '@', '>', '<', '&', '%', '-', '/', '#'],
    re = new RegExp("[" + delimiters.join("").replace(/[-\\\]]/g, "\\$&") + "]", "g");
str = str.replace(re, "");

Comments

1

In your case, I would write a function that escapes any character that could have a special meaning in a regexp, applies the regexp and returns the result:

String.prototype.exclude = function(blacklist) {
    for(var i=0; i<blacklist.length; i++) {
        if(blacklist[i].match(/\W/)) {
            blacklist[i] = '\\'+blacklist[i];
        }
    }
    return this.replace(new RegExp('['+blacklist.join('')+']', 'g'), '');
};

var myString = "j a\nv\ra?s!c;r.i,p`t:I(s)A{w}e[s]o|m'e\\~=@><&%-/#"; 

myString.exclude([' ', '\r', '\n', '?', '!', ';', '.', ',', '`', ':', '(', ')', '{', '}', '[', ']', '|', '\'', '\\', '~', '=', '@', '>', '<', '&', '%', '-', '/', '#']);

// returns "JavascriptIsAwesome" which is of course the correct answer

Comments

0

This might be too aggressive.

/[^A-z0-9]/g

2 Comments

I want to replace only those characters (strictly) mentioned in the list.
OK, in which case don't use the above.

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.