1

I have a jquery code that trims the leading and trailing character (passed from the calling program). I am using a variable in RegExp to replace the character with blank. How can I make the RegExp work for any character passed from the calling program? Here is the simplified code:

var time = ":1h:45m:34s:";
var chr= ':'; //can have . or , or any other character
var regex = new RegExp("(^" + chr + ")|(" + chr+ "$)" , "g"); //works for colon but not for dot.
//var regex = new RegExp("(^/" + chr + ")|(/" + chr+ "$)" , "g"); //for dot I added / but not for colon.
var formattedtime = time.replace(regex, "");

Expected Outputs:

1. time = ":1h:45m:34s:"; 
chr = ":";
Output: 1h:45m:34s
2. time = "1h:45m:34s"; 
chr = ":";
Output: 1h:45m:34s
3. time = ".45m.34s"; 
chr = ".";
Output: 45m.34s
4. time = "1h.45m.34s."; 
chr = ".";
Output: 1h.45m.34s

How can I make the regexp work for any character?

0

3 Answers 3

2

You need to escape meta characters (like . and several others) to get literals. You do that by adding a backslash before them.

JS doesn't have any built in function for that, so you could use this:

function quotemeta(str){
    return str.replace(/[.+*?|\\^$(){}\[\]-]/g, '\\$&');
}

Used like so:

new RegExp("^(?:" + quotemeta(chr) + ")+|(?:" + quotemeta(chr) + ")+$" , "g");
Sign up to request clarification or add additional context in comments.

4 Comments

I did that as in the commented line in my code snippet. var regex = new RegExp("(^/" + chr + ")|(/" + chr+ "$)" , "g"); This works great for dot but not for colon. You mean I have to check the character and escape for each???
/ is not a backslash, \ is. But you can't use it on every character, because they have special meaning, eg \s means any white space.
Ah, this utility method is great. It can handle many characters. More generic approach. How can I understand the RegExp you provided?
It just matches any number of those characters at the beginning or end of string. I used a non-capturing group (?:...) in case it's not just one character, but a string. If you want to remove many different characters at once you can use a character class instead of the group, like [abc] will match one of a, b or c.
0
var chr= ':/';
...    
var regex = new RegExp("(^[" + chr + "])|([" + chr+ "]$)" , "g");

4 Comments

This is a cool solution. It works for me. I am not that good with RegExp. Do you mind explaining this?
Use [] to define a set of characters, eg [:/.] matches on : / or .
I think I misunderstood your question, as I thought you wanted to remove a set of characters by using only one expression.
Pat, sorry if I made it sound silly.. Qtax's solution seems to be working for various characters and I wanted a generic solution to handle different characters. Thanks for your time.
0

Regex should this way:-

For Colon, /^(\:)|(\:)$/gim

For Dot, /^(\.)|(\.)$/gim

OR

/^(\:|\w|\.)|(\:|\w|\.)$/gim

LIVE DEMO

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.