简体   繁体   English

正则表达式(javascript)提取以某个常量字符串开头的字符串

[英]Regular expression(javascript) to extract a string preceded by some constant string

I am trying to extract a string which is preceded by some constant string, the example is as follows. 我试图提取一个字符串,该字符串的前面是一些常量字符串,示例如下。

string ="deleteabc@bcd" 
match should be "abc@bcd"
or
string = "deletebcd@def"
match should be "bcd@def"

so as you can see i want to extract anything after the constant string "delete", please help, thanks in advance 因此,如您所见,我想在常量字符串“删除”之后提取任何内容,请提供帮助,在此先感谢

If you just want to remove a prefix 如果您只想删除前缀

function removePrefix(str, prefix) {
    if (str.search(prefix) === 0) {
        return str.substr(prefix.length);
    } else {
        return str;
    }
}
string = "deleteabc@bcd"
result = string.match(/^delete(.+)/)
console.log(result[1])

abc@bcd ABC @ BCD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM