1

I'm using DataTables (http://www.datatables.net/) to generate a table. Each row has a corporation name, which in most cases is the state, province, or country name. I use chosen.js to generate a multi-select with all of the unique names. The problem is that when I'm searching for "Virginia" I'm also getting results for "West Virginia". I only want "Virginia".

For each option selected in the multi-select, I append it to a regex, e.g.:

\b(Arizona|Virginia)\b

My data table contains rows with the following:

<tr><td>Arizona</td></tr>
<tr><td>Michigan</td></tr>
<tr><td>Virginia</td></tr>
<tr><td>West Virginia</td></tr>
....

What does my regular expression need to be so "Virginia" returns only "Virginia" and not "West Virginia"?

I'm using the DataTables column search (with smart searching off) to execute the regex: https://datatables.net/reference/api/column().search()

2
  • 1
    try anchors ^(Arizona|Virginia)$ Commented Apr 13, 2015 at 14:36
  • 2
    And why you parse this with regex? Commented Apr 13, 2015 at 14:37

1 Answer 1

2

You should use <td> tag as boundaries (if they are part of the string that goes into the regex engine) and only use captured group 1:

 <td>(Arizona|Virginia)</td>

If the string comes in as plain Arizona, or West Virginia, you can use anchors matching at the beginning ^ or end $ of the string:

 ^(Arizona|Virginia)$
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Worked like a charm! +1
Done. Apparently you need to wait 10 minute to accept.
Yes, there is this 10-minute delay to allow for better answers.

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.