Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove everyting after second occurrence of a specific character?

I have a string like below

var str = "Lorem Ipsum is simply& dummy text of the &printing and & typesetting industry."

How can I remove the second character & and everything after it in jQuery?

like image 506
Mona Coder Avatar asked Oct 20 '25 02:10

Mona Coder


1 Answers

You can combine split and join:

str.split("&", 2).join("&")

The argument 2 will make that split only returns the first two substrings that are split off, the rest is ignored. join would put those 2 pieces back together with the restored &.

like image 199
trincot Avatar answered Oct 22 '25 03:10

trincot