将逗号后的文本删除到行尾

Strip text after comma to the end of line

本文关键字:删除 文本      更新时间:2023-09-26

我需要去掉逗号之后的所有文本到行尾。例如,我有一个这样的文本:

1, 2, 3 'n橙色,白色,蓝色

,我需要得到这样的东西:

' n橙色

您可以使用一个非常简单的正则表达式:

replace(/,.*/g, '')

var s = "one, two, three'n orange, white, blue";
console.log(s.replace(/,.*/g, ''));

:

  • , - match , then…
  • .* - 0+除换行符以外的字符
  • /g -多次

用空字符串替换

const string = "one, two, three'n orange, white, blue";
const commaArray = string.split(',');
const lineBreakArray = commaArray[2].split(''n');
const myString = commaArray[0] + "'n" + lineBreakArray[1]; // one'n orange