I need some assistance with correctly implementing a method that uses .split and .replace to format a string value passed to it.
The string value is 90% of the time an HTML link as per this example: <a href=\"http://www.fnb.co.za\" rel=\"nofollow\">SMC RBJacobs</a>. But sometimes it only contains a normal string value such as: SMC RBJacobs.
I thought it would be a good idea to evaluate the value by checking .indexOf('</a>'), but this evaluates to true all the time.(?) I then get an error in the next statements executed in my method.
Here is my method:
if (col.name === 'twitter_tweet.source') {
var str = value;
if (value != null || value != undefined) {
if (value.indexOf('</a>')) {
var parts = str.split('"nofollow\">');
var value = parts[1].replace('</a>', '');
return value;
}
}
return value;
}
The desired output regardless of whether the string contains HTML markup or not: SMC RBJacobs (or whichever name is passed)
Any help would be appreciated!
.indexOf()returns-1if the search value has not been found, hence theifcondition is wrong and should beif (value.indexOf('</a>') > -1) { ... }