Javascript, JQuery: Using # with regex

<p id="abc_0" name="me"> Me </p>
<p id="abc_0" name="me2"> Me2 </p>

Here, there are 2 identical ids.
var ids=$("#abc_0").attr("name");  //returns Me.
This will return the first occurrence, which is what i want.
How do i do this with a regex, eg, /abc/  
Something like : var ids=$('#'+/abc/).attr("name"); This doesn't work though.
The regex should match abc_0, but and the jquery id should return only the first match.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
You can do it like this:

Open in new window

Just be careful with this kind of selectors as they are much heavier that searching directly by ID or even by class.
On a powerful machine and latest browser you won't feel anything but on the other hand, if you run it on a slow machine or a low-end mobile device and your page has a lot of controls, you might feel some pain.

If you really have to do this, at least consider adding a scope to your selector like:
$('[id*=abc]', '#mycontainer').first()

Open in new window

Where your HTML would be:
<div id="mycontainer">
    <p id="abc_0" name="me">Me</p>
    <p id="abc_0" name="me2">Me2</p>

Open in new window

Like this you specify that the search is only to be done inside #mycontainer div which will result in a much faster execution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Just a note on your html markup - it is not legal.

ID's must be unique - if you are going to duplicate ID's on a page you are asking for trouble - and if you find you are in a situation where you are having to do a select on a duplicate ID time to go back to the drawing board and redesign your code.

While the solution by Alex will work - any solution that is based on illegal markup cannot be guaranteed to work everywhere always.

Suggestion: change your markup to have unique ID's
renisenbbAuthor Commented:
Thanks Julian. Actually i am working on old code that had non-unique ids. Hopefully it can all be updated one day.
I agree with Julian here, using a regex to make up for technically incorrect html is not a great idea, as it will work only most of the time. The best solution is to assign meaningful classes to the elements so that you can group them functionally and stylistically.

ultimately, you would want something like this:

<p class="abc_0" id="me" name="me"> Me </p>
<p class="abc_0" id="me2" name="me2"> Me2 </p>


var ids=$(".abc_0").attr("name");
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.