...

View Full Version : Putting quotation marks around text in a string



NanakiXIII
08-22-2008, 02:34 PM
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



{some text, some more text, 5.32, 7.643, yet more text}


to be converted to



{"some text", "some more text", 5.32, 7.643, "yet more text"}


if possible. Does anyone see a way to make this happen?

ohgod
08-22-2008, 04:05 PM
do you mean you don't want the quotes to be parsed?

use a slash to escape them, \"some text\"

Philip M
08-22-2008, 04:25 PM
Here you are:-


<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.

Cranford
08-22-2008, 07:25 PM
------

Philip M
08-22-2008, 07:58 PM
<!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".

oesxyl
08-22-2008, 08:05 PM
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

Philip M
08-22-2008, 08:28 PM
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".

oesxyl
08-22-2008, 08:44 PM
But it seems that the OP does not want numbers in quotes.
op already have a better solution, yours, :)

probably op want also to trimm spacesbecause of \s I thought that already trim, :)

str = str.replace(/{([a-z\s]+)/i, '{"$1"')
best regards

Philip M
08-22-2008, 09:05 PM
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);

NanakiXIII
08-22-2008, 09:41 PM
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 &#235;. 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?

Philip M
08-23-2008, 10:04 AM
The full list of accented characters is:

&#192;&#193;&#197;&#198;&#199;&#200;&#201;&#202;&#203;&#204;&#205;&#206;&#207;&#208;&#209;&#210;&#211;&#212;&#213;&#214;&#216;&#217;&#218;&#219;&#220;&#221;&#222;&#223;&#224;&#225;&#226;&#227;&#228;&#229;&#230;&#231;&#232;&#233;&#234;&#235;&#236;&#237;&#238;&#239;&#240;&#241;&#242;&#243;&#244;&#245;&#246;&#248;&#249;&#250;&#251;&#252;&#253;&#254;&#255;

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



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);
}

NanakiXIII
08-23-2008, 12:54 PM
Thanks a lot, that explains it. My code is working flawlessly now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum