Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > String replace and regexp

Reply
Thread Tools

String replace and regexp

 
 
Fabian Vilers
Guest
Posts: n/a
 
      02-06-2006
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,
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      02-06-2006
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)
 
Reply With Quote
 
 
 
 
marss
Guest
Posts: n/a
 
      02-06-2006
>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 §.

 
Reply With Quote
 
Fabian Vilers
Guest
Posts: n/a
 
      02-06-2006
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!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~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,
 
Reply With Quote
 
marss
Guest
Posts: n/a
 
      02-06-2006
> 'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~CF§Khaki~ANC';
>


var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~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')

 
Reply With Quote
 
Fabian Vilers
Guest
Posts: n/a
 
      02-06-2006
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?
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-06-2006
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)
 
Reply With Quote
 
Fabian Vilers
Guest
Posts: n/a
 
      02-06-2006
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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-06-2006
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!K haki~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)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-06-2006
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!K haki~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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
How to write replace string for object which will be substituted?[regexp] ryniek90 Python 1 08-06-2009 08:16 PM
Re: How to write replace string for object which will be substituted?[regexp] MRAB Python 3 08-05-2009 11:40 AM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
replace a string delimited by 2 other string, regexp problem Sébastien Maurette Ruby 3 10-02-2006 09:40 PM



Advertisments