简体   繁体   English

正则表达式搜索空格后跟一个字符

[英]Regex search for a space followed by a character

var str = 'The quick brown fox @ ';
var str = str.split(/\s/);
var str = str.join('_');

The result of this operation is "The_quick_brown_fox_@_", How can I only split if the space is followed by a character and this character is not a "@"? 该操作的结果是“ The_quick_brown_fox _ @ _”,如何仅在空格后跟一个字符并且该字符不是“ @”的情况下拆分?

Use lookahead: 使用前瞻:

str.split(/\s(?=\w)/);

Yields 产量

["The", "quick", "brown", "fox @ "]

Joins into: "The_quick_brown_fox @ " 加入:“ The_quick_brown_fox @”

var str = str.split(/\s(?!@)/);

试试这个正则表达式

/\s[^@A-Za-z]/

use this regex (?!@)(?=[^$]) 使用此正则表达式(?!@)(?=[^$])

space 空间

(?!@) next symbol not @ (?!@)下一个符号不是@

(?=[^$]) ignoring last lone spase (?=[^$])忽略最后的孤单spase

you can use like this without using Regex 你可以这样使用而无需使用正则表达式

var str = 'The quick brown fox @ ';
var n=str.replace(/ /g,"_");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM