0

I want to split a string by slash (/) but I don't need spilt which strings contains w/,

strings like below

str1 = 'LC,w/SD-FEC_25-NO_DE( CLASS: BF) / LC,w/SD-FEC_25-NO_DE( CLASS: BF)'
str3 = 'LC,w/SD-FEC_25-NO_DE( CLASS: BF)/LC,w/SD-FEC_25-NO_DE( CLASS: BF)'

expected output : ['LC,w/SD-FEC_25-NO_DE( CLASS: BF)' , 'LC,w/SD-FEC_25-NO_DE( CLASS: BF)']

str2 = 'LC,w/SD-FEC_25-NO_DE( CLASS: BF)'

expected output : ['LC,w/SD-FEC_25-NO_DE( CLASS: BF)']

GOOG
  • 73
  • 1
  • 9
  • 4
    If there are always spaces around the slash, you can split at " / ": `s.split(" / ")` Otherwise you can split using a regex, which excludes slashes having a "w" before them, using negative lookbehind: `s.split(/(?<!w)\//)` – kol Nov 24 '20 at 08:38
  • Does this answer your question? [How do I split a string with multiple separators in javascript?](https://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Lety Nov 24 '20 at 08:40

2 Answers2

0

You can use the follow regex to split your string

str2 = 'LC,w/SD-FEC_25-NO_DE( CLASS: BF)'
str2.split(/(?<!w)\//)

So in this regex, we are basically saying that check for something not equal to w(!w). Which is then followed by a '/'. Since it is a regular expression, we escape the '/' by putting a '' in front of it

Akash
  • 762
  • 7
  • 25
0
Follow these instructions
<script>
var str1 = "LC,w/SD-FEC_25-NO_DE( CLASS: BF) / LC2,w/SD-FEC_25-NO_DE( CLASS: BF2)";
var str1 = 'LC,w/SD-FEC_25-NO_DE( CLASS: BF) / LC,w/SD-FEC_25-NO_DE( CLASS: BF)';

var newStr = str1.replace(/w\//gi,"###").split('/');
console.log(newStr);

let store = []
newStr.forEach(function(val){   
    var getVal = val.replace(/###/gi,"w\/")
    //console.log('getVal = ',  getVal);
    store.push(getVal);
})
console.log('store', store);
</script>