1

i've got a function that matches all letters after a space. I mean;

"this is my string" --> tims

just doing this:

$scope.getLetters = function(str) {
            var matches = str.match(/\b(\w)/g);
            var acronym = matches.join('');
            return acronym;
        }

where str of course is the string that i pass from the html. It works pretty well but i would extract max 2 characters. So if the string is

"this is my string" 

i would ti only and not the others. How can i do it?

1 Answer 1

1

Use slice before join:

var str = "this is my string";
str.match(/\b\w/g).slice(0,2).join('')
//=> "ti"
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.