Reputation: 763
I've got a textfield with a bunch of punctuation which is always surrounded by spaces. So that for instance:
I don't recall saying ' pick up the boot ' or ' now is the time ' . But it's possible , so I may have .
What is the regular expression syntax to get rid of these spaces? I'm working in actionscript.
Edit: it's actually both leading and trailing, as you can see from the above example.
Upvotes: 0
Views: 780
Reputation: 47618
Try this:
var str:String = "I don't recall saying ' pick up the boot ' or ' now is the time ' . But it's possible , so I may have .";
// "foo ' bar ' baz" => "foo 'bar' baz"
var re1:RegExp = /'\s(.*?)\s'/g;
str = str.replace(re1, "'$1'");
// "foo , bar . baz" => "foo, bar. baz"
var re2:RegExp = /\s([.,])/g;
str = str.replace(re2, "$1");
For this particular string you'll get that result:
I don't recall saying 'pick up the boot' or 'now is the time'. But it's possible, so I may have.
May be the first regexp need some adjustments as it won't work for string like
I didn ' t say ' foo '.
as it'll be converted to
I didn 't say' foo '.
Short explanation for special symbols used in first regexp:
\s
is for space character (space, tab).
means any character*
zero or more repetition?
makes *
quantifier non-greedy, so it'll match as less characters as possible $1
You can find more information about regexps on this site or in this section of docs from Adobe.
Upvotes: 1