我知道jQuery $ .trim()函数,但我需要的是一种只从字符串的END修剪空白的方法,而不是从头开始。
所以
str =" this is a string ";
会变成
str =" this is a string";
有什么建议吗?
谢谢!
答案 0 :(得分:31)
您可以使用正则表达式:
str = str.replace(/\s*$/,"");
它用字符串替换字符串末尾的所有空格。
故障:
\s*
:任意数量的空格$
:字符串的结尾有关正则表达式的更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
答案 1 :(得分:0)
对于某些浏览器,您可以使用:
str = str.trimEnd();
要么
func CreateK8sClientFromCluster(cluster *gkev1.Cluster) {
decodedClientCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientCertificate)
if err != nil {
fmt.Println("decode client certificate error:", err)
return
}
decodedClientKey, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientKey)
if err != nil {
fmt.Println("decode client key error:", err)
return
}
decodedClusterCaCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClusterCaCertificate)
if err != nil {
fmt.Println("decode cluster CA certificate error:", err)
return
}
config := &rest.Config{
Username: cluster.MasterAuth.Username,
Password: cluster.MasterAuth.Password,
Host: "https://" + cluster.Endpoint,
TLSClientConfig: rest.TLSClientConfig{
Insecure: false,
CertData: decodedClientCertificate,
KeyData: decodedClientKey,
CAData: decodedClusterCaCertificate,
},
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("failed to get k8s client set from config: %s\n", err)
return
}
}
如果您希望整个浏览器覆盖,请使用正则表达式。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd