views:

30

answers:

2
   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 the start pattern till the end pattern

      My expected output will be ##55##data1!##77##data44!!
+2  A: 

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,"");
Sachin Shanbhag
@Sachin:if the data changes ,how is the replace to be made?
Hulk
I think the question is about how to extract data item 66 from the string, regardless of what is contained in 'data'.
fredley
@Hulk - Everytime do you know what data to replace? If yes, then you can have a function for replace and pass this data as argument. Is that ok? or if you can explain in detail the situtation in your question, it would be helpful.
Sachin Shanbhag
@fredley:you are right.
Hulk
@Hulk you might want to edit your question and phrase it a little more clearly
fredley
@Hulk - The edited question still does not give any information on what is required. Its a bit confusing. If its about extracting data 66 from the string given, there should be a definite pattern which you need to tell us in the question from which you can extract data. Something like do you want to get all data enclosed within ## and ## or something like that.
Sachin Shanbhag
@Sachin hehehe. you asked for an edit, you got it! lol
Reigel
@Hulk - Edited my answer. Please check now if this is what you are looking for. I tested this, and it gives your expected output.
Sachin Shanbhag
@Sachin:Excellet answer.Thanks
Hulk
A: 

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

a.replace(/##66##data!!/g,"")
annakata
I think you've misunderstooed the question, it is about parsing the string to get the value of 'data' for item ##66##.
fredley
if the data changes to a string say"abcdfeg" ,how is the replace to be made?
Hulk
@fredley - I don't think that's at all clear. It explicitly says removal and sounds like removal based on a dynamic value, but it could be extraction admittedly.
annakata
@Hulk - if `data` is a dynamic value you can either allow for a general match (replace data with `.*?`) or you can build a Regex dynamically on the explicit value you're looking for.
annakata