1

My first stackoverflow question! So let me know if I could have gone a better route getting this information / if I suck at phrasing my questions well in search. :) meanwhile... ONWARDS TO THE QUESTION!

So I have a regex function:

s = "benisasillyhehehe"; //this would be variable
regex = /benisasilly(.*)/;
console.log(s.match(regex));

and what I want to do is log what's appended to "benisasilly" if it exists, and log null if it doesn't. But what ends up happening is that it logs an empty string instead. Is there a way to incorporate this functionality into a regex function, or should I just check it all outside this function? Also, does it even really matter? I mostly just wanted to do it because I have other regex functions that I'm using in the same system that DO give null, so I would prefer them to all have the same output format. Thanks a toooon! :D

Edit: Let me clarify a little more. This is the output I'm hoping for:

[ 'benisasillyhehehe','hehehe', index: 0, input: 'benisasillyhehehe' ]

and then

null

if it doesn't work.

Currently, when it doesn't work, I get:

[ 'benisasilly', '', index: 0, input: 'benisasilly' ]

So actuallyfor me, null vs empty string is important in that I want the match to fail vs having the match return an empty string.

2
  • Do you want to change .* to .+? Commented Oct 27, 2014 at 23:48
  • 1
    According to MDN: Return value = An Array containing the matched results or null if there were no matches. Are you positive it's returning the empty string? [1]: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 27, 2014 at 23:50

2 Answers 2

2

This is my first post as well.

Can you try changing your regex to

regex = /benisasilly(.+)/;

the * returns a blank string because, * means that matching 0 or more characters. While using + means that there should be at least 1 character appended to the original string.

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

1 Comment

Yup that was it! I feel like kind of a derp now 'cuz I've been using +, but didn't even realize that's what it did haha. Thanks a ton! :)
1

So when you try to execute s.match(regex), the object returned is an array of two String values. The first element is your s variable if it contains your regex value. The second element is the text that's been appended to your regex value. If your s value doesn't contain your regex at all, then s.match(regex) will return null. May I suggest you set up your code like this? ->

s = "benisasillyhehehe";
regex = /benisasilly(.*)/;
var match = s.match(regex);
if(match) console.log(match[1] == "" ? null : match[1]);
else console.log(null);

1 Comment

Yup! That'd certainly work! I was mostly looking for an in-regex answer, but thanks! :)

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.