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?
<!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}';
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..
$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:-
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