Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a string until a specific character appears in javascript

Tags:

javascript

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?

like image 586
Tsundoku Avatar asked Dec 08 '22 21:12

Tsundoku


2 Answers

You're looking for the indexof(str) method

myStr.substring(0, myStr.indexOf('('))
like image 187
SnailCoil Avatar answered May 26 '23 02:05

SnailCoil


Try this:

var a=$(this).text();
console.log( a.substr(0, a.indexOf('(')) ) );
like image 28
web-nomad Avatar answered May 26 '23 00:05

web-nomad