0

I am trying to match the string 6c81748b9239e96e it's random each time. Using the following code below. My problem is that it matches the entire string and I only need the random string that contains the letters and numbers.

String

<a href="playgame.aspx?gid=4&tag=6c81748b9239e96e">Play</a>

javascript regex

string.match(/\&tag\=[A-Za-z0-9]+\"\>/i);
5
  • What else than letters and numbers do you have in your string? Commented Jun 6, 2012 at 10:18
  • @Hauns TM i'm not understanding what you are asking? I added the full string Commented Jun 6, 2012 at 10:20
  • Just wrap [A-Za-z0-9]+ in round brackets - ([A-Za-z0-9]+) Commented Jun 6, 2012 at 10:21
  • The unedited question didn't show all the needed information! Commented Jun 6, 2012 at 10:21
  • @HaunsTM that's why it was edited :-) I didn't add anything actually, it was there, but hidden by SO's xss defense. Commented Jun 6, 2012 at 10:26

4 Answers 4

1

Here is my suggestion:

  1. Add the snippet provided by @Artem Barger to your code: https://stackoverflow.com/a/901144/851498 You need to slightly modify it, though (adding the str argument):

    function getParameterByName( name, str )
    {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec( str );
        if(results == null)
            return "";
        else
          return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
  2. Use it this way:

    var str = getParameterByName( 'tag', string );
    

Jsfiddle demo: http://jsfiddle.net/Ralt/u9MAv/

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

Comments

1

You could use Regular Expression Groups to match, and later access the pattern you are after. The regex you will need to use is like so: /\&tag\=([A-Za-z0-9]+)\"\>/i. The round brackets (( and )) will denote the group you want to capture. You can then access the capture group as shown here.

EDIT: Upon closer inspection it seems that you might be using an incorrect regular expression. I am not really used to Javascript regexes but it seems that you are escaping the & and = and>, which is not required. Try this instead: /&tag=([A-Za-z0-9]+)\">/i.

1 Comment

@Mr.1.0: I think that you are using a wrong regular expression. I have tested a new one which seems to work. Please check my updated response.
0
var myregexp = /(&tag=)([A-Za-z0-9]+)\b/img;
var match = myregexp.exec(subject);
while (match != null) {
    for (var i = 0; i < match.length; i++) {
        // matched text: match[i]
    }
    match = myregexp.exec(subject);
}

Comments

0

Your regex

&tag=([A-Za-z\d]+)"

It's simplified (you escaped too much) and parenthesis were added to put the thing you want in group 1

In javascript this becomes

var myregexp = /&tag=([A-Za-z\d]+)"/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}

Explanation

Match the characters “&tag=” literally «&tag=»
Match the regular expression below and capture its match into backreference number 1 «([A-Za-z\d]+)»
   Match a single character present in the list below «[A-Za-z\d]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A character in the range between “A” and “Z” «A-Z»
      A character in the range between “a” and “z” «a-z»
      A single digit 0..9 «\d»
Match the character “"” literally «"»

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.