Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    do you mean you don't want the quotes to be parsed?

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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #4
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    ------
    Last edited by Cranford; 08-22-2008 at 08:54 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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);

  • #10
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?
    Last edited by NanakiXIII; 08-22-2008 at 08:44 PM.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The full list of accented characters is:

    &#192;&#193;&#197;&#198;&#199;&#200;&#201;&#202;&#203;&#204;&#205;&#206;&#207;&#208;&#209;&#210;&#21 1;&#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


    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

  • Users who have thanked Philip M for this post:

    NanakiXIII (08-23-2008)

  • #12
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a lot, that explains it. My code is working flawlessly now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •