473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String replace and regexp

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,
Feb 6 '06 #1
14 1639
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)
Feb 6 '06 #2
>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 §.

Feb 6 '06 #3
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,
Feb 6 '06 #4
> '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')

Feb 6 '06 #5
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?
Feb 6 '06 #6
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)
Feb 6 '06 #7
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.
Feb 6 '06 #8
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)
Feb 6 '06 #9
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)
Feb 6 '06 #10
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)

Feb 6 '06 #11
marss wrote:
'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");


/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
Feb 6 '06 #12

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.

Feb 6 '06 #13
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
Feb 6 '06 #14
JRS: In article <43*********************@reader0.news.be.easynet.n et>,
dated Mon, 6 Feb 2006 09:52:07 remote, seen in
news:comp.lang.javascript, Fabian Vilers <fv*****@be.keyware.com> 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.
Feb 6 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
1
by: Ed Brandmark | last post by:
I am trying to replace the /'s in an URL with %2F So if the URL was http://www.netscape.com/sports/ I want it to be http:%2F%2Fwww.netscape.com%2Fsports%2F To do this I am trying the following...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
4
by: andrewflanders | last post by:
I have an associative array of keys and values. I want to search a string for the existance of keys and replace them with the values in the array. The problem is that some of the keys resemble...
3
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner...
3
by: mehdi_mousavi | last post by:
Hi folks, Consider the following JavaScript function: function removeParam(str, name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)'); rgx.global = true; rgx.ignoreCase =...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
4
by: Tim Kelley | last post by:
I am reading a text file with a streamreader. I parse out path and need to use that path in a sql statement. The path is in the file as \\servername\share\folder\filename.txt. When the line is...
6
by: kilik3000 | last post by:
How do you do a global replace in a string without using a regex? For example, "ABBA".replace("B", "") returns -"ABA" // I'd like this to be a global replace "ABBA".replace(/B/g, "") ...
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.