1

I am trying to parse a page using javascript this is part of page:

<div class="title">
        <h1>



                    Affect and Engagement in Game-BasedLearning Environments


        </h1>




    </div>

This is link tom page source:view-source:http://ieeexplore.ieee.org/xpl/articleDetails.jsp?tp=&arnumber=6645369?tp=&arnumber=6645369 I am using this:

$(data).find('h1').each(function()
                    {console.log($(this).text());
});

Now I am able to get the value inside header but the value displayed have lots of space in front and back.I tried to replace the whitespace by using replace function but replce isn't happening.I don't understand what is there in front and back of the value of header.I somehow want to remove the extra space.

0

4 Answers 4

1

Replace only replaces the first instance found, it might have only removed one space... try this instead, using regular expression syntax:

text.replace(/ /g, '');

This should remove all spaces, even the ones inside your string text. To avoid this, you may only want to replace double spaces instead:

text.replace(/  /g, '');

Also you may want to remove new lines:

text.replace(/\n/g, '');

Here is an example JSFiddle

If you know for sure that your string is only surrounded on either end by spaces, but you want to preserve everything inside, you can use trim:

text.trim();
Sign up to request clarification or add additional context in comments.

Comments

0

Since your already using jQuery, you can take advantage of their $.trim function which removes leading and trailing whitespace.

$(data).find('h1').each(function() {
    console.log($.trim($(this).text()));
});

Reference: $.trim()

2 Comments

Yeah!That worked like a charm.But I wan't to know what exactly is there before and after the text..whitespaces?
@user3868494 Potentially. It could be newline characters, spaces (including non-breaking spaces) or tabs. Any one or a combination of all these could cause the space you're seeing.
0

Try using Javascript's Trim() to get rid of the whitespaces that's present on both sides.

Function's Reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

Comments

0

Your HTML actually contains these paces within your <h1> element, so it is to be expected that they are present in the result of .text().

Normally you'd just use .trim(). However, you'll likely want to replace line breaks inside the text as well.

$(data).find('h1').each(function() {
  var text = $(this).text();
  // Replaces any multiple whitespace sequences with a single space.
  // Even inside the text.!
  // E.g. " \r\n \t" -> " "
  text = text.replace(/\s+/g, " ");
  // Trim leading/trailing whitespace.
  text = text.trim();
  console.log(text);
});

Fiddle for your pleasure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.