Javascript 删除引号内文本前后的单引号内的空白
我正在使用一个名为QueryBuilder的第三方javascript插件 问题是在保存后无法修剪输入,因此数据会像这样保存Javascript 删除引号内文本前后的单引号内的空白,javascript,jquery,regex,Javascript,Jquery,Regex,我正在使用一个名为QueryBuilder的第三方javascript插件 问题是在保存后无法修剪输入,因此数据会像这样保存 testName=' test ' 这是我的javascript代码,它删除了所有我不想要的空格,我试图删除所有文本前后单引号中的空格。很像一个trim,但是trim不起作用,所以我需要一个正则表达式来替换这个方法 get_condition_sql__str = $.trim(get_condition_sql.sql); ge
testName=' test '
这是我的javascript代码,它删除了所有我不想要的空格,我试图删除所有文本前后单引号中的空格。很像一个trim,但是trim不起作用,所以我需要一个正则表达式来替换这个方法
get_condition_sql__str = $.trim(get_condition_sql.sql);
get_condition_sql__clean = get_condition_sql__str.replace(/\s/g, '')
console.log(get_condition_sql__clean);
jQuery('.exception_conditions__sql').val(get_condition_sql__clean);
在字符串结尾前向前查找一个“”:
const input=`testName='test'`;
const cleaned=input.replace(/+(?=[^']*'$)/g',);
控制台日志(已清理)代码>在字符串结尾前向前看一个”
:
const input=`testName='test'`;
const cleaned=input.replace(/+(?=[^']*'$)/g',);
控制台日志(已清理)代码>请使用此代码删除剩余空间:
<script type="text/javascript">
var original_str3 = " This is a string"
//Strips all space to the left of the string
alert( original_str3.trimLeft() + ' <--- Without any spaces on the left' );
</script>
<script type="text/javascript">
var original_str4 = "This is a string "
//Strips all space to the right of the string
alert( original_str4.trimRight() + ' <--- Without any spaces on the right' );
<script type="text/javascript">
var original_str2 = "S t r in g"
//Strips excessive white spaces i.e. retains only one space between each letter
var white_space_stripped_str = original_str2.replace(/\s+/g, ' ');
alert(white_space_stripped_str + ' <---- With exactly one space between each letter in case each letter has multiple spaces');
</script>
或者您可以根据需要使用etc:
https://www.w3schools.com/jsref/jsref_replace.asp
请使用此选项删除左侧空间:
<script type="text/javascript">
var original_str3 = " This is a string"
//Strips all space to the left of the string
alert( original_str3.trimLeft() + ' <--- Without any spaces on the left' );
</script>
<script type="text/javascript">
var original_str4 = "This is a string "
//Strips all space to the right of the string
alert( original_str4.trimRight() + ' <--- Without any spaces on the right' );
<script type="text/javascript">
var original_str2 = "S t r in g"
//Strips excessive white spaces i.e. retains only one space between each letter
var white_space_stripped_str = original_str2.replace(/\s+/g, ' ');
alert(white_space_stripped_str + ' <---- With exactly one space between each letter in case each letter has multiple spaces');
</script>
或者您可以根据需要使用etc:
https://www.w3schools.com/jsref/jsref_replace.asp
第二个很好,保留了中间的空间,就像我需要它,从末端移除,但只有一个小问题是它离开了前面的空间。尽管如此,还是很好,最让人抱怨的还是结尾的空间,所以非常感谢!运行片段-它看起来要移除前面的空间,第二个工作得很好,保留中间的空间,就像我需要它和从末端移除,但只有一个小问题是它离开前面的空间。尽管如此,还是很好,最让人抱怨的还是结尾的空间,所以非常感谢!运行代码段-它看起来会根据需要删除前面的空格。唯一的问题是字符串实际上是像example=“vSourceCountry='TEST'”那样存储的,所以trim不会在单引号内进行修剪,而只是在double@MichaelDaSilva谢谢,请使用这个:!DOCTYPE html>单击按钮以将下面段落中的“蓝色”替换为“红色”:“vSourceCountry='TEST'”
尝试使用函数myFunction(){var str=document.getElementById(“demo”).innerHTML;var res=str.replace(/'/g,”);document.getElementById(“demo”).innerHTML=res;}@MichaelDaSilva谢谢,如果它对我们有好处,那么请让我知道是否需要进行其他更改。唯一的问题是字符串实际上是像example=“vSourceCountry='TEST'”那样存储的,所以trim不会在单引号内进行修剪,只会在double@MichaelDaSilva谢谢,请使用这个:!DOCTYPE html>单击按钮以将下面段落中的“蓝色”替换为“红色”:“vSourceCountry='TEST'”
尝试使用函数myFunction(){var str=document.getElementById(“demo”).innerHTML;var res=str.replace(/'/g,”);document.getElementById(“demo”).innerHTML=res;}@MichaelDaSilva谢谢如果这对我们有好处,那么如果需要其他更改,请告诉我