0

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?

3
  • 1
    string.split(productNo )[1] Commented Feb 6, 2019 at 15:36
  • 1
    I mean, if you know the string starts with "<stuff><br/>" then really you just need to find the index of the first <br/> and slice after it Commented Feb 6, 2019 at 15:38
  • I assume you want Alice's message too? Commented Feb 6, 2019 at 15:48

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

7 Comments

Missing the second one
What is missing? It looks like it matches his second snippet? @mplungjan
I might be wrong, but I assume OP also wants the second product ID
Given the makup of the string, my assumption would be he might want to stick it on the page and let the html render for David to see his messages. In which case keeping the <br> can make sense. @mplungjan
@Devora if the string format always starts with @stuff<br/> then match1 will always be the order number and match2 will always be all the text after then first product number. Regardless of comment count.
|
1

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);

4 Comments

This works well, i am just not sure if <br\> is saved to mongo, and when i retrieve it, will it have <br\> as part of the string?
@Devora I have not worked with MongoDB. So i'm not sure about that, but I believe there will be no problem.
any idea how to split if the message is like this? theres hidden shift+enter string a = "@David 0044332 Awesome product! (John) 2013-09-02 0044332 Delivered on time (Alice) 2014-02-26"
@Devora have you tried a.split(/\n/); or a.split(/\r/); ?
0

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(" "))
});
  

1 Comment

Yes i do wan Alice aswell, but just keep the rest of it string as it is, only remove the @David and the first <br\> string customercomment = "0044332 Awesome product! (John) 2013-09-02<br/>0044332 Delivered on time (Alice) 2014-02-26"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.