如何在Javascript中格式化字符串?

如何在Javascript中格式化字符串?,javascript,regex,format,Javascript,Regex,Format,我想用Javascript格式化字符串。关于格式,我的意思是: 删除字符串开头和结尾的空格或制表符。 删除字符串中单词之间的多个空格。 按顺序使用逗号 我知道我可以用正则表达式实现这一点,但不知道如何实现 现在: var myString=你好,你好吗; 之后我需要什么: myString=你好,你好吗?; 我试过这个: var myString=你好,你好吗; myString.replace/^\s*|\s{2,}|\s$/; console.logmyString; 但我得到了: 如

我想用Javascript格式化字符串。关于格式,我的意思是:

删除字符串开头和结尾的空格或制表符。 删除字符串中单词之间的多个空格。 按顺序使用逗号 我知道我可以用正则表达式实现这一点,但不知道如何实现

现在:

var myString=你好,你好吗; 之后我需要什么:

myString=你好,你好吗?; 我试过这个:

var myString=你好,你好吗; myString.replace/^\s*|\s{2,}|\s$/; console.logmyString; 但我得到了:

如果你知道,请帮忙。
提前谢谢

我已经用3个正则表达式解决了您的问题,但是第一个是通过JavaScript中已经存在的String.trim方法完成的

var myString=你好,你好吗; myString=myString .trim//修剪起点和终点 .replace/[\t\n]+/g',//将空格减少为一个空格 .replace/[,\?\.\!\;]/g,“$1”;//删除指向字符前的一个空格 console.logmyString 让myString=你好,你好吗; myString=myString.replace/\s{2,}/g'; myString=myString.replace/[,]/g','; myString=myString.replace/[!]/g'!'; myString=myString.replace/[?]/g'?'; myString=myString.replace/[.]/g'; document.getElementById'test'.innerHTML=myString.trim;
使用单个正则表达式的另一种方法:

var myString=你好,你好吗; myString=myString.replace/^\s+|\s+$|\s+?![a-z]/g;
console.logmyString 您可以通过将以下正则表达式的匹配项替换为空字符串来生成所需的字符串

^[ \t]*|[ \t](?=[ \t,?])|[ \t]*$
Javascript的常规引擎执行以下操作

^            : match beginning of string
[ \t]*       : match 0+ spaces or tabs
|            : or
[ \t]        : match a space or tab
(?=[ \t,?])  : next character must be a space, tab, comma or question mark
|            : or
[ \t]*       : match 0+ spaces or tabs
$            : match end of string
?=[\t,,]是一个积极的前瞻

我假设,与示例中一样,问号前面的所有空格和制表符以及逗号都将被删除。如果还应删除其他字符前面的空格和制表符,则应将这些字符添加到字符类[\t,?]。我们可以写,例如,[\t,,!:;]


与所有空白字符匹配的特殊字符\s不应代替[\t]。这是因为\s也匹配换行符。例如,如果字符串是abc\n x,则使用\s将生成字符串abcx,而使用[\t]将字符串转换为abc\nx。

我使用“一次替换”来完成此操作

var myString = "     Hello  , how are         you   ?    ";
myString = myString.replace(/(\s+(?=[,\.?!])|^\s+|\s+$|\s+(?=\s))/g, "")
console.log(myString);
//Expected output: "Hello, how are you?"
代码是将几个正则表达式组合成一个正则表达式。因为我们想用更多的空间来代替。为了避免意外的输出,我们需要在特殊情况下使用

myString.replace(/(^\s+|\s+$)/g, ""); //same as
myString.trim();

myString.replace((\s+(?=[\.,!?])), "");
//Will only matches spaces (which following .,!?)
//So only spaces will be replace

myString.replace((\s+(?=\s)), "");
//same as with following ,.!? But this case with space

//see on the regex replaced with same that is "", so we can combine them together
myString.replace(/(\s+(?=[,\.?!])|^\s+|\s+$|\s+(?=\s))/g, "")

只需使用trim而不是regex,就像这里的专业提示:不要对非HTML和未缩放的括号文本使用innerHTML。另一个提示:在评论中使用@,例如@NiklasE.,以通知收件人。否则你一定希望他们无意中发现你的回答。StackOverflow会在您输入@时自动为您提供建议。默认情况下会通知帖子所有者。
myString.replace(/(^\s+|\s+$)/g, ""); //same as
myString.trim();

myString.replace((\s+(?=[\.,!?])), "");
//Will only matches spaces (which following .,!?)
//So only spaces will be replace

myString.replace((\s+(?=\s)), "");
//same as with following ,.!? But this case with space

//see on the regex replaced with same that is "", so we can combine them together
myString.replace(/(\s+(?=[,\.?!])|^\s+|\s+$|\s+(?=\s))/g, "")