count words

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yin99

    count words

    I have some innerHTML stored as a string, and I would like to know how
    many time throughout the whole innerHTML the occurence of "Hello<BR>"
    occurs. I was wondering if RegEx or some elequant method exists?

    Thanks,
    Yin


    For example: InnerHtml = <b>thisis Hello<BR>
    &npsb<script>Al ert("Test")</script>Hello<BR >

    I would like to use Javascript to parse this and return me answer of
    2, since the string
    Hello<BRoccurs exactly twice.
  • Dan Rumney

    #2
    Re: count words

    Yin99 wrote:
    I have some innerHTML stored as a string, and I would like to know how
    many time throughout the whole innerHTML the occurence of "Hello<BR>"
    occurs. I was wondering if RegEx or some elequant method exists?
    >
    Thanks,
    Yin
    >
    >
    For example: InnerHtml = <b>thisis Hello<BR>
    &npsb<script>Al ert("Test")</script>Hello<BR >
    >
    I would like to use Javascript to parse this and return me answer of
    2, since the string
    Hello<BRoccurs exactly twice.
    innerHTML.match (/Hello\<BR\>/g).length

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: count words

      Dan Rumney wrote:
      Yin99 wrote:
      >I have some innerHTML stored as a string, and I would like to know how
      >many time throughout the whole innerHTML the occurence of "Hello<BR>"
      >occurs. I was wondering if RegEx or some elequant method exists?
      >[...]
      >>
      >For example: InnerHtml = <b>thisis Hello<BR>
      >&npsb<script>A lert("Test")</script>Hello<BR >
      >>
      >I would like to use Javascript to parse this and return me answer of
      >2, since the string
      >Hello<BRoccu rs exactly twice.
      >
      innerHTML.match (/Hello\<BR\>/g).length
      `<' and `>' do not need to be escaped. Also, if there is no match,
      String.prototyp e.match() will return `null' and `null' has no properties
      which is why the `length' property access would result in a TypeError.
      Therefore:

      (InnerHtml.matc h(/Hello<BR>/g) || "").length

      (ECMAScript identifiers are case-sensitive.)


      PointedEars
      --
      Prototype.js was written by people who don't know javascript for people
      who don't know javascript. People who don't know javascript are not
      the best source of advice on designing systems that use javascript.
      -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

      Comment

      • david.karr

        #4
        Re: count words

        On Jun 5, 5:46 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        Dan Rumney wrote:
        Yin99 wrote:
        [deleted]
        innerHTML.match (/Hello\<BR\>/g).length
        >
        `<' and `>' do not need to be escaped. Also, if there is no match,
        String.prototyp e.match() will return `null' and `null' has no properties
        which is why the `length' property access would result in a TypeError.
        Therefore:
        >
        (InnerHtml.matc h(/Hello<BR>/g) || "").length
        Wouldn't that return a count of 1 when it didn't match anything?

        Comment

        • Dan Rumney

          #5
          Re: count words

          david.karr wrote:
          [snip]
          >Therefore:
          >>
          > (InnerHtml.matc h(/Hello<BR>/g) || "").length
          >
          Wouldn't that return a count of 1 when it didn't match anything?
          No

          (Checked with Firebug)

          alert("".length ) displays '0'

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: count words

            david.karr wrote:
            Thomas 'PointedEars' Lahn wrote:
            >Dan Rumney wrote:
            >>Yin99 wrote:
            >>[deleted]
            >>innerHTML.mat ch(/Hello\<BR\>/g).length
            >`<' and `>' do not need to be escaped. Also, if there is no match,
            >String.prototy pe.match() will return `null' and `null' has no properties
            >which is why the `length' property access would result in a TypeError.
            >Therefore:
            >>
            > (InnerHtml.matc h(/Hello<BR>/g) || "").length
            >
            Wouldn't that return a count of 1 when it didn't match anything?
            My Magic 8 Ball says: Unlikely.

            (Since when has the empty string a length of 1 instead of 0?)


            PointedEars
            --
            Use any version of Microsoft Frontpage to create your site.
            (This won't prevent people from viewing your source, but no one
            will want to steal it.)
            -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

            Comment

            • Dr J R Stockton

              #7
              Re: count words

              In comp.lang.javas cript message <0f8eb53f-534e-49f1-90c4-2ab9e7f9e26a@2g
              2000hsn.googleg roups.com>, Thu, 5 Jun 2008 15:09:23, Yin99
              <ws@ziowave.com posted:
              >I have some innerHTML stored as a string, and I would like to know how
              >many time throughout the whole innerHTML the occurence of "Hello<BR>"
              >occurs. I was wondering if RegEx or some elequant method exists?
              One way is to use D.replace(/Hello<BR>/g) and see how much shorter
              that is.

              Another : S.match(/(Hello<BR>)/g).length - if the string varies,
              use new RegExp rather than a literal.

              If using .split("Hello<B R>") you may need to be concerned about the
              possibility of it being first or last, and which browser is in use.

              See also in <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#SSRA> .

              It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

              --
              (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
              news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
              <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...