5 Replies Latest reply on Apr 15, 2011 12:39 AM by Adam Cameron.

    ReReplace to insert characters on certain words in a list

    Golden Pelican

      Hey-

       

       

      I have a list of words I want to insert a sting into. Basically, the list of words comes from a ReMatch() that looks for words over 10 characters long then puts a <wbr> with those words every 5 characters. I almost have it but it's not strict enough and will put the <wbr> other places.

      Here's the code:

       

      <!--- This looks at the string of words (note) and makes an array out of any word over 10 characters long.--->

      <cfset rem = #ReMatch("(?i)[a-z0-9+!@##$%^&*()=/?~_-]{10}", note)# />

       

      <!--- This sets the array to a list --->

      <cfset listrem = arrayToList(rem, ",")>

       

      <!--- This looks for the two words in the list (listrem = thisisalongword,anotherlongword) and puts in a <wbr> every 5 characters --->

      <cfset notes_wbr = reReplaceNoCase("#note#", "(['thisisalongword,anotherlongword']{5})", "\1<wbr>", "all")>

       

      Here's the original string (#note#):

      thisisalongword this is a normal word. But this is going to be anotherlongword 123456789

       

      Here's the replaced string (#notes_wbr#):

      thisi<wbr>salon<wbr>gword<wbr> this is a normal word. But this is going<wbr> to be anoth<wb>rerlon<wbr>gword<wbr> 123456789

       

      The problem is the "going<wbr>". I only want the <wbr> within the list that I made, but it's also putting it after "going".

       

      Any help on this would be greatly appreciated.

       

       

        • 1. Re: ReReplace to insert characters on certain words in a list
          Adam Cameron. Level 5

          You're problem is you're trying to do this:

           

          ['thisisalongword,anotherlongword']

           

          And expecting that to match "thisisalongword" as a string.  Square brackets match single characters, so what that expression means is to match ANY ONE of these characters: ',adeghilnorstw.

           

          Which is not what you want.  You want to be using the OR (|) operator.

           

          --

          Adam

          1 person found this helpful
          • 2. Re: ReReplace to insert characters on certain words in a list
            Golden Pelican Level 1

            Thanks for the help, Adam. Now that I understand a bit more, my need for a solution is growing... and I cannont figure it out... hopefully you can give me some pointers.

             

            Basically, I want to take two words that are in a string and insert a <wbr> within those words every 3 characters.

             

            So this string:

            "thisisalongword this is a normal word. But this is going to be anotherlongword"

            Would turn out to be:

            "thi<wbr>sig<wbr>wor<wbr>d this is a normal word. But this is going to be ano<wbr>the<wbr>rlo<wbr>ngw<wbr>ord"

             

            The closest I can get is:

            <cfset notes_wbr = reReplaceNoCase("thisisalongword this is a normal word. But this is going to be anotherlongword", "(thisisalongword|anotherlongword{1})", "\1wbr", "all")>

             

            Makes the string:

            thisisalongword<wbr> this is a normal word. But this is going to be anotherlongword<wbr>

             

            With this code, I can insert a <wbr> every 3 characters within the entire string with this but I only want it on those two words:

            <cfset notes_wbr = reReplaceNoCase("thisisalongword this is a normal word. But this is going to be anotherlongword", "(.{3})", "\1wbr", "all")>

             

            Any idea how I insert the <wbr> every 3 characters within those two words?

             

            • 3. Re: ReReplace to insert characters on certain words in a list
              Adam Cameron. Level 5

              Are they specific words (and a finite list, preexisting), or any long words?

               

              Why are you needing to do this, btw?  It looks like you're mangling your text a bit by doing this... I doubt search engine indexing is going to like what you're doing much (esp as longer words tend to be the more important/relevant words in a doc...).  That said: I have no idea what search engine indexing does in these case.  It might be savvy enough to see a word-break as a zero-width token, in which case it might recompose the word... however I could equally see how you might end up with it treating them as a bunch of (meaningless) three-char sequences.

               

              --

              Adam

              • 4. Re: ReReplace to insert characters on certain words in a list
                Golden Pelican Level 1

                I'm doing it to break really long words that throw off my formatting. This is on an internal site so no seach engine would index it.

                 

                It could be in infinite number of words, but I have the words in a list seperated by a (pipe). Basically the user enters a  sentence into a textarea, but if there are really long words I want to break them with a <wbr> so the formatting isn't messed up. Once the user enters the sentence, here's what I do to the sentance #note#.

                 

                <!--- This grabs all words in the sentence over 15 characters and puts them in an array

                <cfset rem = #ReMatch("(?i)[a-z0-9+!@##$%^&*()=/?~_-]{15}", note)# />

                 

                <!--- This makes the array a list with a (pipe) delimiter

                <cfset listrem = arrayToList(rem, "|")>

                 

                <!--- This is what I can't get working. Basically, #listrem# is all the 15+ character words and I need to find those words in #notes# (which is the full string entered into a text box) then put a <wbr> every three characters within the list that is #listrem#

                <cfset notes_wbr = reReplaceNoCase(#note#, "(#listrem#{1})", "\1wbr", "all")>

                • 5. Re: ReReplace to insert characters on certain words in a list
                  Adam Cameron. Level 5

                  You might be overcomplicating the replace operation.  Just iterate over your match array, then replace "thisismylongwordhere" with "thi<wbr/>sis<wbr/>myl<wbr/>ong<wbr/>wor<wbr/>dhe<wbr/>re".  To create the second string, to a regex replace-all of "(.){3}" with "\1<wbr/>".

                   

                  --

                  Adam