删除字符串开头的特殊字符,然后在javascript中搜索@symbol.in

Remove special character from the starting of a string and search @ symbol.in javascript

本文关键字:javascript 搜索 @symbol in 然后 开头 字符串 特殊字符 删除      更新时间:2023-09-26

我只想删除字符串开头的特殊字符。即,如果我的字符串类似于{abc@xyz.com,那么我想从开始处删除{。这根绳子看起来像abc@xyz.com

但是,如果我的字符串像abc{@xyz.com,那么我希望保留与它相同的字符串,即abc{@xyz.com

此外,我想检查我的字符串是否存在@符号。如果存在,则OK,否则显示消息。

下面演示了您指定的内容(或已接近(:

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;

我使用了两个独立的regex对象来实现您的需求。它检查字符串中的两个条件。我知道它不是很有效,但它会达到你的目的。

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 @");
}

希望这能帮助