4

I am reading from a list of strings like this:

April 9, 2012 (2012-04-09) num 0 April 16, 2012 (2012-04-16) num 0 April 23, 2012 (2012-04-23) num 0 April 30, 2012 (2012-04-30) num 0 May 7, 2012 (2012-05-07) num 0 May 14, 2012 (2012-05-14) num 0 October 8, 2012 (2012-10-08)[126] num 0 October 15, 2012 (2012-10-15)[126] num 0 October 22, 2012 (2012-10-22)[126] num 0 October 29, 2012 (2012-10-29)[126] num 0

I want to read the date until the first parenthesis '(' I've tried:

console.log( $(this).text().substring(0,16) );

but that only works with the longer months, the rest (like may or june) return part of the rest of the string. Is there a method I could use to do this?

Tsundoku
  • 9,104
  • 29
  • 93
  • 127
  • Possible duplicate of [how to grab substring before a specified character jquery or javascript](http://stackoverflow.com/questions/9133102/how-to-grab-substring-before-a-specified-character-jquery-or-javascript) – AXMIM Sep 23 '16 at 18:08

3 Answers3

13

You're looking for the indexof(str) method

myStr.substring(0, myStr.indexOf('('))
Scott
  • 21,211
  • 8
  • 65
  • 72
SnailCoil
  • 818
  • 1
  • 9
  • 23
  • 1
    You beat me to it! I'll add to your answer: here's a jsFiddle example: http://jsfiddle.net/4YsaR/ – DACrosby Sep 11 '12 at 06:05
  • This should be the answer. Ignore mine. – web-nomad Sep 11 '12 at 06:06
  • 1
    For those looking at this several years later as an answer to a similar problem, if there was no bracket this would return an empty string. So need to be careful to check the "stop" character exists first! – John Feb 10 '17 at 09:59
1

Try this:

var a=$(this).text();
console.log( a.substr(0, a.indexOf('(')) ) );
web-nomad
  • 6,003
  • 3
  • 34
  • 49
0

Try console.log( $(this).text().substring(0, $(this).text().indexOf('(') );

basiljames
  • 4,777
  • 4
  • 24
  • 41