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 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replacing multiple string values

    I'm pretty much trying to turn q,r,t into q/r/t to help with some other programs, but I can't seem to get the logic down on this problem; please do not just give me the answer as I'd like to learn it myself; but a push in the right direction is always welcome

    p.s. I do not want to use regexp

    Code:
    function replacement(string, replaced, replacing) {
    	var str = string,
    		rplcd = replaced,
    		rplig = replacing
    	;
    	var qwerty = str.replace(rplcd, rplig),
    		ending = str.indexOf(rplcd),
    		qwertyKey = ending.toString().replace(rplcd, rplig)
            ;
    	if (qwerty.charAt(1) !== "/") {
    		return console.log("not gonna work buddy");
    	}
    	else {
    		qwerty = qwerty + qwertyKey;
    		if (qwerty === qwerty.lastIndexOf("/")) {
    			return console.log(qwerty);
    		}
    		else {
    			return console.log("not gonna work buddy");
    		}
    	}
    }
    Last edited by JonBMN; 02-26-2013 at 10:06 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    It's a bit hard to not give the complete answer for something that involves so little code. The following will replace any alphanumeric character followed by a comma with the same character followed by a slash and so converts 'q,r,t' into 'q/r/t'.

    'q,r,t'.replace(/(\w),/g,'$1/')

    I assume that the string you posted was just an example and so you'd substitute the variable name containing the real string to be converted as the string on which the replace is to be run.


    The string.replace method can take a regular expression as the first parameter. A regular expression provides a pattern against which the parts of the string are matched with any match then being replaced by the value in the second parameter.

    To specify a regular expression you surround it inside / /

    \w in a regular expression means any letter or number

    The g after the second / means repeat the replace for every match found.

    The ( ) around part of the regular expression captures whatever it contains and allows it to be copies into the output - the first ( ) is referenced in the output string as $1 (if there were a second one it would be $2 and so on).

    With your example string the replace call finds and replaces two occurrences of a letter followed by a comma with that same letter followed by a /
    Last edited by felgall; 02-26-2013 at 09:19 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, and I understand what your saying and getting the correct answer by using a regexp but in my case I'd like to not use a regexp which I don't think I said in my first post.
    Last edited by JonBMN; 02-26-2013 at 09:23 PM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by JonBMN View Post
    but in my case I'd like to not use a regexp which I don't think I said in my first post.
    Why do people insist on not using a screwdriver to insert screws and insist on using a hammer instead.

    For pattern matching (which is what your question is asking how to do) a regular expression is the appropriate tool for the job.

    The only reason to not use a regular expression for this is if it is homework for a class where the teacher is handing out questions intended to tech you how to use a hammer to insert screws rather than providing questions where the answer actually needs to use the type of code that the question is supposed to be testing. But then you didn't say that this is homework so I am at a complete loss as to why you want to write many lines of inefficient code where a single statement does the job.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is a homework assignment where I do not get to decide whether or not to use a screwdriver or a hammer. If I could I would be using a screwdriver for many of my programs, but that is not up to me.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,647
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    It's homework. Look at his other questions.

    But he doesn't really need a regular expression for this, if his actual input is as limited as he says.
    turn q,r,t into q/r/t
    simply
    Code:
    var s1 = "q,r,t";
    var s2 = s1.replace(",","/");  // only replaces first comma
    var s3 = s2.replace(",","/");  // then the second one
    Now, Jon: Figure out how to use that information to make sure you have replaced *ALL* commas, no matter how many there are.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by JonBMN View Post
    If I could I would be using a screwdriver for many of my programs, but that is not up to me.
    Sounds like you already know more about how to use JavaScript properly than the person teaching your class does.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @felgall I don't know about that I'm still a newbie, but I'd like to think so haha

    @Old Pendant Thank you for the help! I will be sure to figure how to do it for all of them and not just a simple c,d,c string.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by JonBMN View Post
    @felgall I don't know about that I'm still a newbie, but I'd like to think so
    Well while you may be a newbie you are already thinking about what JavaScript commands are appropriate for which tasks whereas your teacher is obviously not considering that at all since they gave you homework where you are not allowed to use the most appropriate code for the job.

    As your teacher isn't giving enough thought to making sure that the homework questions match the type of code they are intended to test means that you have to wonder what other aspects of JavaScript programming have they overlooked in what they are teaching. Any question that involves pattern matching should be left to the part of the course dealing with regular expressions.


    What you need to do to turn Old Pedant's code into a complete solution is to incorporate the replace statement into a loop. Presumably you have been taught at least one of the four ways to create loops in JavaScript.
    Last edited by felgall; 02-26-2013 at 10:23 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @felgall and @old pedant you two are helping me out greatly in making me think like a javascript programmer and the logic behind it. I appreciate both of your help greatly!


  •  

    Posting Permissions

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