0

I searched around and after running this through a few RegEx builders it seems to do what I want... but when I actually try and implement it it never seems to work.

This is the expression I'm using: (?<=\[)[0-9]+(?:\.[0-9]*)?(?=\])

From my understanding, this is supposed to match any numbers between the [] symbols. I am unconcerned if it is a true float, positive or negative, or any of that, because it is predetermined.

The purpose of this is that I have a set number of prebuilt <select> tags that the website has already built. I need to loop through all of the select tags and get the currently selected option, and inside of that match the number between [].

<select>
   <option>Stuff [193.33]</option>
   <option>Stuff2 [19232l.39393]</option>
</select>

Something like...

$('#content_area').$('input:select').change(function(){
   $('#content_area').$('select option:selected').each(function(){}

I am unaware as to why,

  1. the expression gives me a syntax error even if I just return it in an alert, and
  2. how would I construct the loop?
1
  • 1
    Did you mean to have the letter l in <option>Stuff2 [19232l.39393]</option>? I ask because your regex won't match any letters. Commented Jan 10, 2011 at 23:57

4 Answers 4

2

Your jQuery code is completely wrong.

You're probably trying to write

$('#content_area select').change(function() {
    var value = $(this).val();
});
Sign up to request clarification or add additional context in comments.

Comments

0

a) You should put this as the value of your option, e.g.

<select>
   <option value="193.33">Stuff [193.33]</option>
   <option value="19232l.39393">Stuff2 [19232l.39393]</option>
</select>

b) If you can't change your HTML, then the regex you want is:

/\[\d+(\.\d+)?\]/

You can't use what you have for several reasons, including the fact that most JS interpreters do not not support positive lookbehind assertions. Because of this, we'll have to capture the square brackets and then ignore them, like so:

$('foo option:selected').each(function(){
  var float = /\[(\d+(\.\d+)?)\]/.exec(this.value)[1] * 1;
});

1 Comment

@AnonymousDownvoter I would appreciate it if you would explain why you felt my answer was unworthy of a '1' ranking. Drive-by downvoting can help clarify a large pool of answers, but it is much better to leave a comment telling others why you feel they should steer clear of this answer.
0

According to this article Javascript does not support lookbehind. Would this expression work for you?

(?:\[)([0-9]+(?:\.[0-9]*)?)(?:\])

EDIT: Or, more concisely \[(\d+(?:\.\d*)?)\] (Thanks to Phrogz for pointing this out)

EDIT2: To address the jQuery bit, maybe something like this would work (lifted from the jQuery :selected documentation):

$("select").change(function () {
    $("select option:selected").each(function () {
        var value = $(this).text();
        // Do regex magic
    });
});

3 Comments

In a regex with non-capturing groups, (?:\\[) is the same as simply \\[. Also, you forgot to escape the literal period in the original.
There is a capturing group in the middle to grab the digits. I tried replacing the (?:\\[) with \\[ but it doesn't seem to match correctly. I'm not a regex guru so it could be that I'm doing something silly. Like the literal . non-escaping :)
@Phrogz: No, I stand corrected. It does work when one does it right :)
-1

There is a great tool here to test your regex's

http://www.regextester.com/

2 Comments

This is a comment, not an answer.
This should really be a comment, not an answer. You're just providing a tool, not really answering his question.

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.