I have an exact string below
string a = "@David<br/>0044332 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"
I am trying to split the above string into the result below
string productNo = "0044332"
string customercomment = "0044332 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"
How can i split it to remove the @David, get the productNo and then the rest as the comment from string a?
You could use replace()
to match on the pieces you want and pull them out.
var test = "@David<br/>0044332 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"; var orderNumber; var comment; test.replace(/^[^>]+<br\\/>(\\d+)(.+)$/, function(_, match1, match2){ orderNumber = match1; comment = match1 + match2; }); console.log(orderNumber); console.log(comment);
If you first split your string
by <br>
then you can get an array with the different data:
const a = "@David<br/>0044332 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"; let data = a.split(/<br\\/>/); console.log(data);
Now, for get the productNo
you can perform a String::match() to get the first match of sequential numbers on any of the elements of the previous array that isn't at index 0
. You have all the messages on the array indexes 1 to array.length
, but if you need to get they together again, you can Array::join() they back.
const a = "@David<br/>0044332 Awesome product 123! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"; let data = a.split(/<br\\/>/); console.log(data); // Get product number from string on index 1. let productNo = data[1].match(/\\w+/)[0]; // Join back all messages. let customerComments = data.slice(1).join("<br>"); // Show information. console.log(productNo); console.log(customerComments);
I assume you also want Alice's bits?
const a = "@David<br/>0044331 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26" const parts = a.split("<br/>") parts.shift() console.log(parts) parts.forEach(function(part) { let bits = part.split(" "); console.log(bits[0],":",bits.slice(1).join(" ")) });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.