String replace and regexp

Discussion in 'Javascript' started by Fabian Vilers, Feb 6, 2006.

  1. Hi all,

    I'm looking for a regular expression that can do the following:

    if matches: a~b§c~d
    replace by: a~§c~

    Sorry for posting something so stupid but I can't understand a word on
    regexp :(

    Thanks in advance,
    Fabian Vilers, Feb 6, 2006
    #1
    1. Advertising

  2. Fabian Vilers

    Evertjan. Guest

    Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:

    > Hi all,
    >
    > I'm looking for a regular expression that can do the following:
    >
    > if matches: a~b§c~d
    > replace by: a~§c~
    >
    > Sorry for posting something so stupid but I can't understand a word on
    > regexp :(


    If you do not understand, how would you possibly know it is stupid,
    or that regex would be a good solution?

    At least you would have to declare what you mean by ~ and §.

    As I read it now, this code will do what you ask:

    s = 'a~b§c~d'
    alert(s)
    s = 'a~§c~'
    alert(s)



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 6, 2006
    #2
    1. Advertising

  3. Fabian Vilers

    marss Guest

    >if matches: a~b§c~d
    >replace by: a~§c~


    If to take your sample as real data:

    var s = "bla-bla~b§c~done";
    var s1 = s.replace(/a~b§c~d/g, "a~§c~");

    But if it is only sample I agree with Evertjan: you would have to
    declare what you mean by ~ and §.
    marss, Feb 6, 2006
    #3
  4. marss wrote:

    > But if it is only sample I agree with Evertjan: you would have to
    > declare what you mean by ~ and §.
    >


    Thanks all.

    I can give you more explanation here.

    I've a string containing this:

    var data_table =
    'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC';

    What I need is:

    If the regexp matches a~b§c~d, where a, b, c, d can be any combination
    of letters, replace it by a~§c~ or said differently, drop b and d.

    Am I much clear with theses information?

    Thanks again in advance for helping,
    Fabian Vilers, Feb 6, 2006
    #4
  5. Fabian Vilers

    marss Guest

    > 'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC';
    >


    var s =
    "Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";
    var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");


    > What I need is:
    >
    > If the regexp matches a~b§c~d, where a, b, c, d can be any combination
    > of letters, replace it by a~§c~ or said differently, drop b and d.
    >


    But it is not clearly stated what to do when we have a~b§c~d§e~f
    pattern (your data string ends with
    Magenta~RECUP§PaleGreen~CF§Khaki~ANC')
    marss, Feb 6, 2006
    #5
  6. marss wrote:

    > var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");


    Thanks! You're a regexp god ;-)

    >
    > But it is not clearly stated what to do when we have a~b§c~d§e~f
    > pattern (your data string ends with
    > Magenta~RECUP§PaleGreen~CF§Khaki~ANC')
    >


    Yes, forgot to say :(

    I can have 4 possibilities:
    - a~b
    - a~b§c~d
    - a~b§c~d§e~f
    - a~b§c~d§e~f§g~h

    If it's one the last three, b, d, f and h must be removed.

    How can I change the regexp?
    Fabian Vilers, Feb 6, 2006
    #6
  7. Fabian Vilers

    Evertjan. Guest

    Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:

    > marss wrote:
    >
    >> var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

    >
    > Thanks! You're a regexp god ;-)
    >
    >>
    >> But it is not clearly stated what to do when we have a~b§c~d§e~f
    >> pattern (your data string ends with
    >> Magenta~RECUP§PaleGreen~CF§Khaki~ANC')
    >>

    >
    > Yes, forgot to say :(
    >
    > I can have 4 possibilities:
    > - a~b
    > - a~b§c~d
    > - a~b§c~d§e~f
    > - a~b§c~d§e~f§g~h
    >
    > If it's one the last three, b, d, f and h must be removed.
    >
    > How can I change the regexp?
    >


    This still is not right.

    We should know what the first letter not in the string after b,d,f or h is

    Or:

    a~b§c~da~b§c~d

    what is the delimiter between d and a?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 6, 2006
    #7
  8. Evertjan. wrote:
    > a~b§c~da~b§c~d
    >
    > what is the delimiter between d and a?
    >


    It depends. It could be either a '!' or a ';'.

    In fact, a got a lot of information inside a string. My problem (see
    other post) is that I worked with the string splited into arrays. But
    looping in these arrays was throwing the warning message box about
    infinite loops.
    Fabian Vilers, Feb 6, 2006
    #8
  9. Fabian Vilers

    Evertjan. Guest

    Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:

    > Evertjan. wrote:
    >> a~b§c~da~b§c~d
    >>
    >> what is the delimiter between d and a?
    >>

    >
    > It depends. It could be either a '!' or a ';'.


    Please always specify fully!

    > In fact, a got a lot of information inside a string. My problem (see
    > other post) is that I worked with the string splited into arrays. But
    > looping in these arrays was throwing the warning message box about
    > infinite loops.
    >



    <script type='text/javascript>

    var s =
    "Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;"+
    "Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";

    s = s.replace(/([^~§!;]+~)[^~§!;]+([~§!;]||$)/g,'$1$2')

    alert(s)

    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 6, 2006
    #9
  10. Fabian Vilers

    Evertjan. Guest

    Evertjan. wrote on 06 feb 2006 in comp.lang.javascript:

    > Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
    >
    >> Evertjan. wrote:

    >
    > <script type='text/javascript>
    >
    > var s =
    > "Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;"+
    > "Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";
    >
    > s = s.replace(/([^~§!;]+~)[^~§!;]+([~§!;]||$)/g,'$1$2')


    Even simpler, just delete anything between ~ and § or ! or ; or EOL:

    s = s.replace(/~[^~§!;]+([§!;]||$)/g,'~$1')


    > alert(s)
    >
    > </script>
    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 6, 2006
    #10
  11. Fabian Vilers

    marss Guest

    Fabian Vilers wrote:

    > I can have 4 possibilities:
    > - a~b
    > - a~b§c~d
    > - a~b§c~d§e~f
    > - a~b§c~d§e~f§g~h
    >
    > If it's one the last three, b, d, f and h must be removed.
    >
    > How can I change the regexp?


    If I 've got it right pattern a~b should not be changed.

    var s1 = s.replace(/(§\w+~)\w+([!;]|$)/g,
    "$1$2").replace(/(\w+~)\w+(§)/g, "$1$2");


    > If the regexp matches a~b§c~d, where a, b, c, d can be any combination
    > of letters


    If you assure that a, b, c, d contains only letters, number or
    underscore use \w,
    if it can contain other symbols use [^~§!;] insead of it (as Evertjan
    suggested)
    marss, Feb 6, 2006
    #11
  12. marss wrote:

    >>

    'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC';
    >>

    >
    > var s =
    >

    "Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";
    > var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");


    /x*/ matches the empty string, too, for any /x/. To fulfill the OP's
    requirement --

    >> If the regexp matches a~b§c~d, where a, b, c, d can be any combination
    >> of letters,


    -- where it remains to be discussed what characters are regarded as letters,
    \w matches only _ASCII_ letters --

    >> replace it by a~§c~ or said differently, drop b and d.


    -- use the `+' quantifier instead of the `*' quantifier.

    And please learn to quote.

    <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
    <URL:http://www.safalra.com/special/googlegroupsreply/>


    PointedEars
    Thomas 'PointedEars' Lahn, Feb 6, 2006
    #12
  13. Fabian Vilers

    marss Guest

    Thomas 'PointedEars' Lahn wrote:

    > > var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

    >
    > /x*/ matches the empty string, too, for any /x/. To fulfill the OP's
    > requirement --
    >


    Why is the wrong answer cited when the corrections are already made?

    >
    > And please learn to quote.
    >


    I am learning.
    marss, Feb 6, 2006
    #13
  14. marss wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> > var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

    >>
    >> /x*/ matches the empty string, too, for any /x/. To fulfill the OP's
    >> requirement --

    >
    > Why is the wrong answer cited when the corrections are already made?


    Probably because USENET is not a real-time communication medium :)


    PointedEars
    Thomas 'PointedEars' Lahn, Feb 6, 2006
    #14
  15. JRS: In article <43e70e3c$0$438$>,
    dated Mon, 6 Feb 2006 09:52:07 remote, seen in
    news:comp.lang.javascript, Fabian Vilers <> posted
    :
    >I'm looking for a regular expression that can do the following:
    >
    >if matches: a~b§c~d
    >replace by: a~§c~
    >
    >Sorry for posting something so stupid but I can't understand a word on
    >regexp :(



    An example is rarely sufficient to define a task. Describe it in words,
    and then give examples to illustrate those words. You can also post in
    both English and French, which may well help understanding.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Feb 6, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ryniek90
    Replies:
    1
    Views:
    228
  2. MRAB
    Replies:
    3
    Views:
    268
    Jon Clements
    Aug 5, 2009
  3. Sébastien Maurette
    Replies:
    3
    Views:
    136
    David Vallner
    Oct 2, 2006
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    479
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    354
    7stud --
    Aug 21, 2009
Loading...

Share This Page