删除标点符号 javascript/jquery 之前的空格
Remove space before punctuation javascript/jquery
我想在Javascript/jquery中的每个标点符号之前删除空格。例如
Input string = " This 's a test string ."
Output = "This's a test string."
"This string has some -- perhaps too much -- punctuation that 's not properly "
+ "spaced ; what can I do to remove the excess spaces before it ?"
.replace(/'s+('W)/g, "$1");
//=> "This string has some-- perhaps too much-- punctuation that's not properly "
// + "spaced; what can I do to remove the excess spaces before it?"
将 String.replace
函数与正则表达式一起使用,该正则表达式将匹配要匹配的所有标点字符之前的任意数量的空格:
var regex = /'s+([.,!":])/g;
var output = "This 's a test string .".replace(regex, '$1');
如果要使用正则表达式,请匹配
/'s'./
并仅用一个点替换它。
尝试替换 。
var test = "This's a test string";
test = test.replace(" 's", "'s");
OutPut = test;
var str= "This 's a test string ."
var regex = /'s''/i;
var output =str.replace(regex, "'");
如果要
从字符串中删除特定的标点符号,最好明确删除所需的标点符号
replace(/['.,-'/#!$%'^&'*;:{}='-_`~()]/g,"")
执行上述操作仍然不会返回您指定的字符串。如果你想删除删除疯狂标点符号留下的任何额外空格,那么你将想要做一些类似的事情
replace(/'s{2,}/g," ");
我的完整示例:
var s = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";
var punctuationless = s.replace(/['.,-'/#!$%'^&'*;:{}='-_`~()]/g,"");
var finalString = punctuationless.replace(/'s{2,}/g," ");
尝试像
var my_arr = [];
my_arr = my_str.split("'");
var output = $.trim(my_arr[0]) + "'" + $.trim(my_arr[1]);
alert(output);
看到这个小提琴 但首先,尝试一些东西。
相关文章:
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- Jquery:如何获取所选选项全文(带空格)
- jQuery字符串使用split()方法在空格后拆分字符串
- 突出显示jQuery中单词之间的空格
- jquery textcomplete无法使用Unicode字符,并且缺少空格
- jQuery animate 会中断空格键滚动
- 我想限制用户在jquery中输入文本后面的空格,如果它在文本后面,我会删除空格
- 仅在jQuery验证中验证数字、括号和空格
- 按下空格键一次后,Jquery功能不起作用
- 通过JQuery在网页上的两个字符之间放置空格
- 通过JavaScript或jQuery删除P标记,其中包含不间断的空格
- 如何在jquery中使用replace和regex在单词的大写字符前插入空格
- jQuery从数组中删除空格
- JavaScript/jQuery:在特殊字符和关闭html标记后插入空格
- 删除标点符号 javascript/jquery 之前的空格
- jqgrid子网格row_id空格不起作用PHP jquery
- javascript / jQuery “insertAfter” 函数参数中的空格会导致失败
- 发送带有空格的 jQuery 获取
- 如何将制表符替换为四个空格 jQuery
- 只允许字母和空格-jquery验证插件