Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2008, 01:34 PM   PM User | #1
NanakiXIII
Regular Coder

 
Join Date: Dec 2003
Posts: 100
Thanks: 1
Thanked 0 Times in 0 Posts
NanakiXIII is an unknown quantity at this point
Putting quotation marks around text in a string

I'm looking for a way to put quotation marks around pieces of text in a string that also contains decimal numbers, commas and curly brackets. For example, I would want

Code:
{some text, some more text, 5.32, 7.643, yet more text}
to be converted to

Code:
{"some text", "some more text", 5.32, 7.643, "yet more text"}
if possible. Does anyone see a way to make this happen?
NanakiXIII is offline   Reply With Quote
Old 08-22-2008, 03:05 PM   PM User | #2
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
do you mean you don't want the quotes to be parsed?

use a slash to escape them, \"some text\"
ohgod is offline   Reply With Quote
Old 08-22-2008, 03:25 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-

Code:
<script type = "text/javascript">
str = "{some text, some more text, 5.32, 7.643, yet more text}"
str = str.replace(/{([a-z\s]+)/i, '{"$1"')
str = str.replace(/([0-9\.]+)/g, '"$1"');
str = str.replace(/(\,\s)([a-z\s]+)/gi, '\, "$2"');
alert (str);
</script>
Delete the line in red if you do not want the numbers to be in quotes.


It is not as cold as it was yesterday, but that's probably because it's a bit warmer. Sports commentator.

Last edited by Philip M; 08-22-2008 at 03:28 PM..
Philip M is offline   Reply With Quote
Old 08-22-2008, 06:25 PM   PM User | #4
Cranford
Banned

 
Join Date: May 2005
Location: Midwest, U.S.
Posts: 118
Thanks: 1
Thanked 26 Times in 23 Posts
Cranford is an unknown quantity at this point
------

Last edited by Cranford; 08-22-2008 at 08:54 PM..
Cranford is offline   Reply With Quote
Old 08-22-2008, 06:58 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Cranford View Post
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

	var nStr = '{some text, some more text, 5.32, 7.643, yet more text}';

	function init(){
		
		nStr = nStr.replace(/([a-z\s]+),\s([a-z\s]+),\s([\d\.\s,]+)\s([a-z\s]+)/,'"$1", "$2", $3 "$4"')
		alert(nStr)
	}

	onload = init;
		
</script>
</head>
	<body>
		
	</body>
</html>
That only works for the one string:-
var nStr = '{some text, some more text, 5.32, 7.643, yet more text}';
and not for e.g.
var nStr = '{some text, 2.56, some more text, 5.32, 7.643, yet more text}';

"If it ain't broke, keep on at it until it is".

Last edited by Philip M; 08-22-2008 at 07:01 PM..
Philip M is offline   Reply With Quote
Old 08-22-2008, 07:05 PM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Code:
function conv(astr){
    var ar = astr.split(/,/);
    astr = ar.join('","').replace(/^\{/,'{"');
    return astr.replace(/\}$/,'"}');
}
some things +/-:
+ any number of items comma separated
- quote numbers
- don't trim spaces before and after
- assume astr format is correct

best regards
oesxyl is offline   Reply With Quote
Old 08-22-2008, 07:28 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by oesxyl View Post
Code:
function conv(astr){
    var ar = astr.split(/,/);
    astr = ar.join('","').replace(/^\{/,'{"');
    return astr.replace(/\}$/,'"}');
}
some things +/-:
+ any number of items comma separated
- quote numbers
- don't trim spaces before and after
- assume astr format is correct

best regards
But it seems that the OP does not want to have numbers in quotes.


"If it ain't broke, keep on at it until it is".

Last edited by Philip M; 08-22-2008 at 07:45 PM..
Philip M is offline   Reply With Quote
Old 08-22-2008, 07:44 PM   PM User | #8
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
But it seems that the OP does not want numbers in quotes.
op already have a better solution, yours,

Edit: probably op want also to trimm spaces
Edit: because of \s I thought that already trim,
Code:
str = str.replace(/{([a-z\s]+)/i, '{"$1"')

best regards

Last edited by oesxyl; 08-22-2008 at 08:32 PM..
oesxyl is offline   Reply With Quote
Old 08-22-2008, 08:05 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by oesxyl View Post
probably op want also to trimm spaces
Perhaps - but he did not ask for that. If that is required add to my script:-

str = str.replace(/\"\s+/g, '"');
str = str.replace(/\s+\"/g, '"');

alert (str);
Philip M is offline   Reply With Quote
Old 08-22-2008, 08:41 PM   PM User | #10
NanakiXIII
Regular Coder

 
Join Date: Dec 2003
Posts: 100
Thanks: 1
Thanked 0 Times in 0 Posts
NanakiXIII is an unknown quantity at this point
Thanks for all your replies, I appreciate it. Philip M, your piece of code is great. It's nearly perfect for what I want to do and it's only not perfect because I didn't give enough information about what I needed. I hope you can help me perfect it.

Cranford, your code would indeed work for this particular string but I also need it to work for different ones.

oesxyl, your approach was something I was trying at first as well, but it doesn't work, first of all because I don't want the numbers quoted and second of all because I won't be working with just one string like the example I posted. That's of course again an error on my part, I did not provide enough information. Thanks, though.


As to Philip M's piece of code, it also doesn't work for more than one of these strings (I've actually got a set of these types of string which are also comma-seperated) but that was easily fixed by adding a global flag. However, the text I'm manipulating doesn't contain only the basic a-z letters, but also letters with accents, such as ë. Is there a specific subset of characters that I can include to extend the functionality of the code to these characters, or will I have to add them manually?

EDIT: Oh, I forgot the most important thing I wanted to post about. You're using $1 and $2. I'm assuming these are placeholders for what you are replacing, or something? I was looking for something like that but I didn't have any luck finding anything like this. It's of course perfect for my purpose, but would someone tell me how they work, or link me somewhere or just tell me what these things are called so I can compose a search phrase that will actually get me some information?

Last edited by NanakiXIII; 08-22-2008 at 08:44 PM..
NanakiXIII is offline   Reply With Quote
Old 08-23-2008, 09:04 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The full list of accented characters is:

ÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ

You will have to add those which are applicable to you manually. Instructions at:-
http://nbii-thesaurus.ornl.gov/thesaurus/accents.html

$1...$9 are properties containing parenthized substrings (if any) from a regular expression.
There is a good tutorial on regular expressions on this site at:-

http://www.javascriptkit.com/javatutors/re.shtml


Code:
function matchDemo(){
   var s;
   var re = new RegExp("d(b+)(d)","ig");
   var str = "cdbBdbsbdbdz";
   var arr = re.exec(str);
   s = "$1 contains: " + RegExp.$1 + "\n";
   s += "$2 contains: " + RegExp.$2 + "\n";
   s += "$3 contains: " + RegExp.$3;
   alert(s);
}

Last edited by Philip M; 08-23-2008 at 09:17 AM.. Reason: Add demo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
NanakiXIII (08-23-2008)
Old 08-23-2008, 11:54 AM   PM User | #12
NanakiXIII
Regular Coder

 
Join Date: Dec 2003
Posts: 100
Thanks: 1
Thanked 0 Times in 0 Posts
NanakiXIII is an unknown quantity at this point
Thanks a lot, that explains it. My code is working flawlessly now.
NanakiXIII is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:12 AM.


Advertisement
Log in to turn off these ads.