2

I have this part of code

<a href='project.php?id=5'>test</a> 

Where id number can have different length and I need to extract only this number. Is it possible through regular expression, or something better?

EDIT

I prob have solution, fitting my problem, what you think about:

string.split('=')[2].split('\'')[0]

When string contains text. I'm sure, that text will always have same pattern

7
  • This is part of your document or is it inside a string? Commented Aug 26, 2013 at 6:36
  • Other than using regular expressions, is there a reason why you are not using data-attributes? It would make your life a bit easier, and your javascript code much cleaner. Commented Aug 26, 2013 at 6:40
  • @Kippie Using data- attributes like this is a horrible broken approach that disregards separation of concerns and how applications should be structured. The ID of the project has nothing to do with the presentation, storing your data on the DOM instead of in JavaScript is a blatent disregard of how an application should be structured. You need to aspire to a single source of truth in your application. Storing data in the DOM instead of in JS objects (like any sensible design in any UI ever does) is a recipe for code spaghetti. Commented Aug 26, 2013 at 6:44
  • This is inside string Commented Aug 26, 2013 at 6:49
  • @BenjaminGruenbaum Could you elaborate on this? How does this go against SoC? Why would it have to do anything with presentation? I understand your point about single source of truth (id being specified in both the href- as well as the data-attribute). However, your point of storing data in the DOM instead of in JS objects is still unclear to me. Commented Aug 26, 2013 at 6:51

2 Answers 2

3

Let's say you have your string "<a href='project.php?id=5'>test</a>"

Using regular expressions to parse HTML can get real ugly real fast (what if the a has more attributes? What if there are multiple tags there?)

The cleanest would be to dump it in a new HTML element and process it as HTML using the powerful native built in DOM API instead of as a string.

var temp = document.createElement("div");
temp.innerHTML = "<a href='project.php?id=5'>test</a>";
var url = temp.firstChild.href; // now contains project.php?id=5

Now we can use more traditional tools to extract the id

var id = url.split("=")[1]; // the first thing after =.

More generally the strategy is:

  • Create an empty element or a fragment and put our HTML there
  • Use DOM querying methods to find our elements, in this case .firstChild was enough but getElementsByTagName or even querySelector might be useful.
  • Once we got the attribute, it's just a simple string, and we can safely use regex for it, or any other string processing method we choose.

If you're unacquainted with the DOM API this tutorial is a good place to start. It's how we process the document with JavaScript and knowing it is important to your success as a front end JavaScript developer.

Fiddle

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

3 Comments

Its interesting, but i really parse string, not html element. And not sure if it will have some problems with performance, make this an element.
"<a href='project.php?id=5'>test</a>" is representing HTML. The performance of this solution should not concern you unless you're doing tens of millions of these extractions every second.
@LubošSuk Getting the correct result is more important then performance. Also, don't worry about performance until it becomes a problem.
0
function parse(url){
    var result = { pathname: null, search: {} };
    var parts = url.split('?');
    result.pathname = parts[0];
    var search = parts[1];
    if(search){
        var ps = search.split('&');
        for(var i = 0; i < ps.length; i++){
            var p = ps[i].split('=');
            result.search[p[0]] = p[1];
        }
    }
    return result;
}

parse('project.php?id=5');

EDIT: To get anchor element:

 var anchors = document.getElementsByTagName('a');
 for(var i = 0; i < anchors.length; i++){
     var a = anchors[i];
     if(a.innerHTML === 'test')
         console.log(parse(a.href));
 }

1 Comment

I don't think OP's problem is getting the ID from the link href itself, rather it's to extract the link from the HTML.

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.