2

I am attempting to make an angularJS filter which will remove timestamps that look like this: (##:##:##) or ##:##:##.

This is a filter to remove all letters:

.filter('noLetter', function()  {
//this filter removes all letters
        return function removeLetters(string){
        return string.replace(/[^0-9]+/g, " ");
        }
        })

This is my attempt to make a filter that removes the time stamps, however it is not working, help is much appreciated.

.filter('noStamps', function () {
  return function removeStamps(item) {
  return item.replace(/^\([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\)$/i, "");
 }
})

My goal is for it to delete the timestamps it finds and leave nothing in their place.

edit based on question in comments: The time stamps are in the text so it would say "this is an example 21:20:19 of what I am 21:20:20 trying to do 21:20:22"

I would want this to be converted into "this is an example of what I am trying to do" by the filter.

1
  • Remove anchors and/or make paren optional? Try \(?[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\)? no need for i flag, no letters, maybe use g flag. Commented Nov 7, 2015 at 14:49

1 Answer 1

1

You may use

/\s*\(?\b\d{2}:\d{2}:\d{2}\b\)?/g

See regex demo

Thre main points:

  • The ^(start of string) and $(end of string) anchors should be removed so that the expression becomes unanchored, and can match input text partially.
  • Global flag to match all occurrences
  • Limiting quantifier {2} to shorten the regex (and the use of a shorthand class \d helps shorten it, too)
  • \)? and \(? are used with ?quantifier to match 1 or 0 occurrences of the round brackets.
  • \s* in the beginning "trims" the result (as the leading whitespace is matched).

JS snippet:

 
var str = 'this is an example (21:20:19) of what I am 21:20:20 trying to do 21:20:22';
var result = str.replace(/\s*\(?\b\d{2}:\d{2}:\d{2}\b\)?/g, '');
document.getElementById("r").innerHTML = result;
<div id="r"/>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.