I have a certain line I want to get from this line the word "USA" which is after the second comma, how can I get it? I have a lot of such lines, I need to get a certain word that is after the second comma
let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
Simply splitting the string on commas and accessing the element after the 2nd one should be enough. As long as the line does indeed contain enough commas you can do:
const x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
const result = x.split(",")[2].trim();
console.log(result);
Please note i called String.prototype.trim() function simply to remove whitespace from the beginning of a string.
You can split the string and get the second index from the value. Use optional chaining .? to avoid the error.
let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
let value = x.split(',')?.[2]?.trim() || ""
console.log(value)