3

I have a large static result and I'm trying the following changes:

  • Replace the original domain to another one.
  • Convert url's hash to url with parameters using the post id only for a specific domain (website.com).

This is my original static result example with 3 links and 2 differents domain names:

var json = {This is the static result with many links like this <a href=\"http://website.com/932427/post/something-else/\" target=\"_blank\"> and this is other link obviusly with another post id <a href=\"http://website.com/456543/post/another-something-else/\" target=\"_blank\">, this is another reference from another domain <a href=\"http://onother-website.com/23423/post/please-ingnore-this-domain/\" target=\"_blank\"> }

So, the originals url's I need to change are two, according with the above example:

http://website.com/932427/post/something-else/ 
http://website.com/456542/post/another-something-else/

And I want to change that links now with this format:

http://other_domain.com/id?=932427/
http://other_domain.com/id?=456543/

And the final result should look like this into the static result.

By the way I'm using node.js

Thanks in advance

6
  • DO you try to do something? Commented Mar 1, 2014 at 16:34
  • Do you mean a redirect? Do you use any framework for the HTTP? Commented Mar 1, 2014 at 16:36
  • json does not look like real JSON. Take a look at regular expressions (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…). You'll then be able to find required links and replace/reformat them in a certain way you need it. Commented Mar 1, 2014 at 17:13
  • @FUserThrowError Sure, I'll trying with this option: var url = "website.com/932427/post/something-else"; var re3 = /\/news.urban360.com.mx\/(\d+)/; var found = url.match(re3); console.log(JSON.stringify(found)); However just change the first coincidence. var found = url.match(re3); Commented Mar 1, 2014 at 17:19
  • @Bergi Nop, i dont looking for redirect, i looking for change the original urls into the result static, on the another hand I use express. Commented Mar 1, 2014 at 17:25

2 Answers 2

4

Node.js has a built in module for parsing and constructing URLs. Your solution can be written as:

var url = require('url'); // Comes with Node.

// Get the path: '/932427/post/something-else/'
var path = url.parse('http://website.com/932427/post/something-else/').path; 

var newUrl = url.format({
    protocol: 'http',
    host: 'other_domain.com',
    query: { id: path.split('/')[1] }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming all links follow the same pattern and your json object looks like this

var json = {
    urls: [
        'http://website.com/932427/post/something-else/',
        'http://website.com/456542/post/another-something-else/'     
    ]
};

You could use a simple regex to extract the ids and construct your new links like this

var idPattern = /\/(\d{6})\//; // matches 6 digits inside slashes
var newUrlFormat = 'http://other_domain.com/id?={id}/';
var newUrls = [];

json.urls.forEach(function (url) {
    var id = idPattern.exec(url)[1];
    newUrls.push(newUrlFormat.replace('{id}', id))
});

See this jsfiddle to try it out.

3 Comments

Thaks, I will try it!.
this will work but is certainly not a robust solution. Much better to use Node.js built-ins, see other answer.
I don't think my solution is less robust, but I agree that using built in NodeJS modules is a better way to go. It also makes for more readable code. I upvoted the other answer.

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.