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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert variable into regular expression

    I am trying to turn selected text into bold text. In order to do that, I need to insert them into an array.
    How do I insert the variables from the Array into the variable containig the regular expression?

    Code:
    jan= new String("The traditional birthstone for February is Amethyst");
    expArray = new Array('Amethyst','The');
    for (i = 0; i < expArray.length -1; i++){
    	regExp = /"+expArray[i]+"{1}/;
    	newString = new String('<b>'+ expArray[i]+'</b>');
    	result = jan.replace(regExp, newString);
    	document.write(result);
    }

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expression Object

    Code:
    Syntax 1
    
    re = /pattern/[flags]
    Syntax 2
    re = new RegExp("pattern",["flags"])
    syntax2...
    Thanks in advance!

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re:

    I'm sorry, I don't quite get it. Could you explain what you wrote up there?

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks in advance!

  • #5
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New Regular Expression issue

    I got this code to Bold selected text, but now it returns the text twice in a row. Like this:

    The traditional birthstone for February is
    AmethystThe traditional birthstone for February is
    Amethyst

    I'm really new to regular expressions, what should I do?

    Code:
    
    jan= new String("The traditional birthstone for February is Amethyst");
    expArray = new Array('Amethyst','The');
    for (i = 0; i < expArray.length; i++){
        regExp=eval('/'+expArray[i]+'{1}/');
        newString = new String('<b>'+ expArray[i]+'</b>');
        result = jan.replace(regExp, newString);
        document.write(result);
    }
    Amethyst

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First read the manual... (some of it, at least).
    Thanks in advance!

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
    jan= new String("The traditional birthstone for February is Amethyst");
    expArray = new Array('Amethyst','The');
    for (i = 0; i < expArray.length; i++){
        regExp=eval('/'+expArray[i]+'{1}/');
        newString = new String('<b>'+ expArray[i]+'</b>');
        jan = jan.replace(regExp, newString);
        }
        document.write(jan);
    </script>

  • #8
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re:

    I am honestly reading the manual, and still having pronblems.
    As far as I can tell, the replace() thinks that it has to write a new line of text for every instance of [i]. SO I guess what I need to figure out is how to make it parse all bold text into a single line.

  • #9
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re:

    Nevermind. Figured it out.
    Thanks one and all!


  •  

    Posting Permissions

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