• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Finding instances of ##foobar##

Using JavaScript, I want var foobar to be the values in the body between ## and ##

For example:
How much  ##wood## does a woodchuck ##chuck## if a ##woodchuck## could chuck wood?

I want:
   foobar[0] = 'wood';
   foobar[1] = 'chuck';
   foobar[2] = 'woodchuck';

0
hankknight
Asked:
hankknight
  • 3
2 Solutions
 
cy-wongCommented:
Hi,
I don't really understand what do you want but please see if the following code helps?
Thank you
<script>
doucment.write('How much '+foobar[0]+' does a woodchuck '+foobar[1]+' if a '+foobar[2]+' could chuck wood?');
</script>

Open in new window

0
 
hankknightAuthor Commented:
No, I want a script to FIND the values between ## and ##

Now your idea returns this:
How much does a woodchuck if a could chuck wood?

So I need JavaScript to find the values of foobar[1] from the body innerHTML
0
 
HonorGodCommented:
Something like this perhaps?
<script type='text/javascript'>
  var text = 'How much  ##wood## does a woodchuck ##chuck## if a ##woodchuck## could chuck wood?'
  var info = text.split( '##' )
  var result = []
  for ( var i = 1; i < info.length; i += 2 ) {
    result[ result.length++ ] = info[ i ]
    doucment.write( info [i] + '<br>' )
  }
</script>

Open in new window

0
 
HonorGodCommented:
As a function:
<html>
<body>
 
<script type='text/javascript'>
  function findit( text ) {
    var info = text.split( '##' )
    var result = []
    for ( var i = 1; i < info.length; i += 2 ) {
      result[ result.length++ ] = info[ i ]
    }
    return result
  }
 
  document.write( findit( 'How much  ##wood## does a woodchuck ##chuck## if a ##woodchuck## could chuck wood?' ) )
</script>
 
</body>
</html>

Open in new window

0
 
HonorGodCommented:
Thanks for the grade & points
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now