views:

34

answers:

1
+2  Q: 

jquery trim value

I have the following JavaScript:

var value = '19-JUL-10 04.36.38.643000 PM-->19-JUL-10 05.06.33.962000 PM' 

How can I use jQuery to trim it to become '19-JUL-10 04.36.38 PM-->19-JUL-10 05.06.33 PM'?

+3  A: 

No need for jQuery, plain ol' JavaScript will do. Use a regular expression with the replace() method:

var value = '19-JUL-10 04.36.38.643000 PM-->19-JUL-10 05.06.33.962000 PM';
alert(value.replace(/\.\d{6}/g, ""));
//-> "19-JUL-10 04.36.38 PM-->19-JUL-10 05.06.33 PM"

The regular expression, /\.\d{6}/g, looks for a . immediately followed by and including 6 consecutive digits (\d{6}). The global switch (/g) is applied to make it replace all (both) occurrences.

Andy E
i was going to suggest substr, but this is far more elegant
Andrew Bullock
also, well done for explaining the parts of the regex :)
Andrew Bullock