I need to convert a string as follows:
tag, tag2, longer tag, tag3
in
tag, tag2, longer-tag, tag3
To do this briefly, I need to replace spaces without commas with hyphens, and I need to do this in Javascript .
I think this should work
var re = new RegExp("([^,\s])\s+" "g"); var result = tagString.replace(re, "$1-");
Edit: Updated after observing Blixt.
mystring.replace(/([^,])\s+/i "$1-"); There's a better way to do this, but I can never remember the syntax
mystring.replace(/([^,])\s+/i "$1-");
[^,] = Not a comma
[^,]
Change Sorry, do not notice the replacement before. I updated my answer:
var exp = new RegExp("([^,]) "); tags = tags.replace(exp, "$1-");
text.replace(/([^,]) /, '$1-');
Unfortunately, Javascript does not seem to support negative images, so you need to use something like this (changed from here ):
var output = 'tag, tag2, longer tag, tag3'.replace(/(,)?t/g, function($0, $1){ return $1 ? $0 : '-'; });
([^,]) - the first character is not a comma, the second character is a space, and it searches for such a string
([a-zA-Z] ){1,}
May be? Not tested. something like that.