Reputation: 113
I'm digging into back-end development (as a UI designer) and wanted to practice what I've learned by using cheerio, request, and express to scrape an astrology website for a paragraph of text. From this site, I pull the selected data to my server and pass it onto the client - which succeeded. The problem I've come against is that on the astrology site, the Date and the Astrology Reading are in the same paragraph tag and I can't separate them. It comes out looking like this:
Jun 7, 2019: Today is a great day to experience new things—they should be both as pedestrian as a new breakfast cereal [...]
I originally considered using the replace method, but have been moving towards the substring method instead. My thoughts were that I could set the starting value to 12, in which case that would account for the minimum of characters in the date. I could then replace any spaces that occur before the first character of the reading (in this case, 'Today'). The problem is that if there are 2 digits for the current day, I risk removing the first character from the reading.
Is this the logic I have to play around with or is there a simpler method that I could use?
Upvotes: 0
Views: 95
Reputation: 321
Use the substr method in conjunction with the indexOf method that will find the index of the first ":" character. Start at the location of the ":" and extract the rest of the string.
finalStr = str2.substr(str2.indexOf(":") + 1)
//returns everything after the ":" at the end of the date string.
Upvotes: 0
Reputation: 37755
You can simply split values by first :
let str = `Jun 7, 2019: Today is a great day to experience new things—they should be both as pedestrian as a new breakfast cereal [...]`
let str2 = `Jun 17, 2019: Today is a great day to experience new things—they should be both as pedestrian as a new breakfast cereal [...]`
let dateAndInfo = (str) =>{
return str.split(/(^[^:]+):/).filter(Boolean).map(e=>e.trim())
}
console.log(dateAndInfo(str))
console.log(dateAndInfo(str2))
Upvotes: 1