1

I want to remove special characters from the starting of the string only. i.e, if my string is like {abc@xyz.com then I want to remove the { from the starting. The string shoould look like abc@xyz.com

But if my string is like abc{@xyz.com then I want to retain the same string as it is ie., abc{@xyz.com.

Also I want to check that if my string has @ symbol present or not. If it is present then OK else show a message.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Are you looking for email validation ? If so. check this http://stackoverflow.com/questions/2783672/email-validation-javascript – Surabhi Jan 15 '13 at 06:11

2 Answers2

1

The following demonstrates what you specified (or it's close):

var pat = /^[^a-z0-9]*([a-z0-9].*?@.*?$)/i; //pattern for optional non-alphabetic start followed by alphabetic, followed by '@' somewhere
var testString = "{abc@xyz.com"; //Try with {abcxyz.com for alert
arr = pat.exec(testString);
var adjustedString;
if (arr != null) { adjustedString = arr[1]; }  //The potentially adjustedString (chopped off non-alphabetic start) will be in capture group 1
else { adjustedString = "";  alert(testString + " does not conform to pattern"); }
adjustedString;
DWright
  • 9,258
  • 4
  • 36
  • 53
  • :- Thanx a lot..but what if I get more than one bracket or any other special character at the beginning ie., {#!abc@xyz.com. I want to eliminate the special characters from the starting only(ie., til the first charcater or integer is received)?? Any help would be highly appreciated!! – Rahul Tripathi Jan 15 '13 at 07:43
  • Hi, I edited the answer in line with your comments. Now, multiple non-alphabetic, non-digit characters will be stripped at the beginning. The way I do that is by using *, which will match 0 such characters if there aren't any, or one or many. – DWright Jan 15 '13 at 08:01
1

I have used two separate regex objects to achieve what you require .It checks for both the conditions in the string.I know its not very efficient but it will serve your purpose.

var regex = new RegExp(/(^{)/);
var regex1 = new RegExp(/(^[^@]*$)/);
var str = "abc@gmail.com";
if(!regex1.test(str)){
     if(regex.test(str))
         alert("Bracket found at the beginning")
      else
        alert("Bracket not found at the beginning")
}
else{
   alert("doesnt contain @");
}

Hope this helps

saraf
  • 646
  • 4
  • 8
  • Also if u need email validation here is a good thread http://stackoverflow.com/questions/46155/validate-email-address-in-javascript . However as the post suggests email validation should also be done at the server side – saraf Jan 15 '13 at 07:09