开发者

Split a string on a pattern

开发者 https://www.devze.com 2023-01-16 19:43 出处:网络
var a=\"##55##data1!!##66##data4545!!##77##data44!!\"; how to remove ##664545##data!! from the string Edit:if we know the start value in a string i.e,##66## and the end value ie,!!
   var a="##55##data1!!##66##data4545!!##77##data44!!";

how to remove ##664545##data!! from the string

Edit:if we know the start value in a string i.e,##66## and the end value ie,!!

In the main string how to remove characters starting from the start pattern,data after t开发者_运维知识库he start pattern till the end pattern

      My expected output will be ##55##data1!##77##data44!!


Using javascript and regex -

a.replace(/##66##[a-zA-Z0-9]*!!/g,"") 

If you want to parameterize this then you can do as given below where start and end are your parameters-

    var a = "##55##data1!!##66##data4545!!##77##data44!!";
    var start = "##66##";
    var end = "!!";

    var re = new RegExp(start + "[a-zA-Z0-9]*" + end, "g");

    return a.replace(re,"");


Regex way, with global flag g to catch all matches:

a.replace(/##66##data!!/g,"")
0

精彩评论

暂无评论...
验证码 换一张
取 消