patad
patad

Reputation: 9682

Regexp: Trim parts of a string and return what ever is left

Im trying to use regexp to get whatever's behind the # in my string "12344dfdfsss#isa", in this case I wanna get the 'isa' out of the string.

I found these answers (How to remove a small part of the string in the big string using RegExp) helpful, but all it returns is 'true'.

var myString = '12344dfdfsss#isa',
    newRG = new RegExp('#(.*)$'),           
    trimmed = newTrim.test(myString);

I want it to retun 'isa' and not true.

Thanks for any help // I

Upvotes: 2

Views: 760

Answers (3)

user213154
user213154

Reputation:

if it's possible that the subject string could have more than one # then consider:

/#(.*)$/

captures everything that follows the first # in the subject string., while

/#(.*?)$/

captures what follows the last.

Upvotes: 1

Brian Campbell
Brian Campbell

Reputation: 333046

You can use also use match to match a string against a regex, and then extract the first subexpression that matched using [1]:

var trimmed = '12344dfdfsss#isa'.match(/#(.*)$/)[1]

Upvotes: 2

SLaks
SLaks

Reputation: 887777

Try this:

var trimmed = /#(.*)$/.exec('12344dfdfsss#isa')[1];

Upvotes: 4

Related Questions