-1

I have this html string x:

Michelle Brook
<br></br>
The Content Mine
<br></br>
michelle@contentmine.org

It is taken from first lines of http://www.dlib.org/dlib/november14/brook/11brook.html

I would like to obtain x.substring(0,14)=Michelle Brook. The problem is that before the M, there are two special characters (unicode code=10) that makes x.substring(0,14)=Michelle Bro.

In fact, using x.split("") i can see {" "," ","M",.....}

I wouldn't remove these characters. I would like to make substring doing the right thing "keeping in mind" that special characters. How could i do? Is there a different javascript function that makes that?

SctALE
  • 509
  • 2
  • 10
  • 30
  • I feel those 2 special characters are carriage return and new line characters. You could just [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) it before using `substring` – MinusFour Jan 31 '16 at 17:45
  • you may check this link, it can be helpful for you http://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string – Kgn-web Jan 31 '16 at 17:45
  • Use a proper html micro template and problem disappears – charlietfl Jan 31 '16 at 17:57

3 Answers3

0

y cant u alote this strimg in a functio and trim start and end of the String.

Nidhin Prathap
  • 696
  • 5
  • 15
0

Use .trim to remove \n (code 10)

The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

x.trim().substring(0,14);

Or using a regex:

var match = x.match(/[\w ]{14}/);
console.log(match[0]);
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
0

From your webpage:

window.onload = function() {
  var arrStr = document.getElementsByClassName('blue')[0].innerHTML.replace(/[^A-Za-z0-9 <>]/g, '').split('<br>');
   alert(arrStr[0].trim());
}
<p class="blue">

    Michelle Brook<br>
    The Content Mine<br>
    michelle@contentmine.org<br><br>

    Peter Murray-Rust<br>
    University of Cambridge<br>
    pm286@cam.ac.uk<br><br>

    Charles Oppenheim<br>
    City, Northampton and Robert Gordon Universities<br>
    c.oppenheim@btinternet.com

    <br><br>doi:10.1045/november14-brook
</p>

With the replace function you can remove any character is out of your interest: in your case I considered you are looking for letters (uppercase, lowercase), numbers and space. You can add other characters to remove.

gaetanoM
  • 41,594
  • 6
  • 42
  • 61