Got this string:
'test',$, #207
I need to remove spaces which have a commma before
So the result will be: 'test',$,#207
Tried this:
replace(/\/s,]/g, ',')
Not working. Any ideas?
由于您的模式很简单,因此只需执行.split(', ').join(',')
To replace only spaces and not other whitespaces
use the following regex.
Regex : /, +/g
Explanation :
,
will search for comma.
+
will search for multiple spaces.
And then replace by ,
using replace(/, +/g, ',')
replace(new RegExp(find, ', '), ',');
I need to remove spaces which have a commma afterwards
No, your example says the opposite. That is, you want to remove spaces that have a comma before them.
In either case, the error in your expression is the "]".
replace(/\/s,/g, ',')
Does what you say you want to do, and
replace(/,\/s/g, ',')
Does what the example says.
The other answer is right, though - just use replace(' ,', '')
; you need no regex here.
I think you meant comma that have whitespace afterwards:
stringVar = "'test',$, #207";
replace('/\,\s/g', stringVar);
\\,
means ,
literally and \\s
means whitespace.
You can test javascript regex and know a little more about the modifiers and stuff at regex101 .
For all whitespaces which have a "," before
var str = "test,$, #207, th, rtt878";
console.log(str.replace(/\,\s+/g,","));
var str = "test,$, #207";
console.log(str.replace(/\,\s+/g,","));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.