1

I have a form on a webpage I am building where a user inputs a URL. The user has the option to enter a URL with two variants either containing PLT or PDW.

Example, www.userurl.plt.com or www.userurl.pdw.com

Now from this input I want to use some javascript to create a URL string based on what the user entered but to include both the entered and non entered.

what I have so far is this.

$(window).on('load', function() {   
    $('#open').click(function() {
        var regex1 = /\.(PLT)\./i;
        var regex2 = /\.(PDW)\./i;
        var fixedData1 = 'http://www.myurl/user/?var-ip1=', 
            fixedData2 = '&var-ip2=',       

            userEntry1 = $('#one').val(),

            replaced = userEntry1.replace(regex1, '.plt.');
            replaced = userEntry1.replace(regex2, '.pdw.');


        var newWindow = window.open(fixedData1 + userEntry1 + fixedData2 + replaced);

        newWindow.focus();
    });
});

I am quite new so probably doing something wrong but this works okay so,

If I entered www.testurl.pdw.com

I would get an output of:

http:‌//www.myurl/user/?var-ip1=www.testurl.pdw.com&var-ip2=www.testurl.plt.com

Which is good!, but if I entered www.testurl.plt.com I get

http:‌//www.myurl/user/?var-ip1=www.testurl.plt.com&var-ip2=www.testurl.plt.com

which is of course bad. Im not looking for anyone to just do it for me but any pointers in the right direction would be great, this is part of a bigger project and this is slowing me down. Many thanks

4
  • There is some issue with the logic at replaced = userEntry1.replace(regex1, '.plt.'); and replaced = userEntry1.replace(regex2, '.pdw.');. Commented Apr 19, 2017 at 19:53
  • still only works one way Commented Apr 19, 2017 at 19:54
  • is regex necessary ? jsfiddle.net/yxb9sush Commented Apr 19, 2017 at 19:59
  • how could that be incorporated from an input though, that is however exactly what i want Commented Apr 19, 2017 at 20:07

2 Answers 2

1

First, switch your regex tests so that you're replacing 'PLT' with 'PDW' and vice versa, as you're intending.

Second, the way that the code is written above is incorrect, because replaced will always have the value of the second replace() call. Instead, you can check the value of replaced is still the same as userEntry1 and do a replace then.

var fixedData1 = 'http://www.myurl/user/?var-ip1=', 
    fixedData2 = '&var-ip2=',       
    userEntry1 = $('#one').val(),
    replaced = userEntry1.replace(regex2, '.plt.');

if (replaced === userEntry1) {
    replaced = userEntry1.replace(regex1, '.pdw.');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this does make sense, my apologies i am quite new to this, it took me long enough to figure out that regex, how to do you mean switch it up? - thanks
@user3236169 No problem! I'll explain your original code: var regex1 = /\.(PLT)\./i; Match a (case-insensitive) string '.plt.' and capture the 'PLT' portion (known as a capture group) -- You're not actually using the group specifically in your instance, so you could lose the brackets. replaced = userEntry1.replace(regex1, '.plt.'); Replace the first instance of the match regex1 and replace it with '.plt.' You're replacing plt with plt, and pdw with pdw in your original example. That's why you want to switch your regex around.
0

You could approach this in different ways. You describe that users either provide an url in the form of:

  1. www.userurl.plt.com, or
  2. www.userurl.pdw.com

Instead of replacing, you could capture only the userurl part of the input string, and build the output string based on that:

"http:‌//www.myurl/user/" +
"?var-ip1=www." + userurl + ".pdw.com" +
"&var-ip2=www." + userurl + ".plt.com"

Demo below:

function convert(inputString){
  var userurl = inputString
    .match(/www\.(\w*)\.(pdw|plt)\.com/i);
  if(userurl){
    userurl = userurl[1].toLowerCase();
    return "http:‌//www.myurl/user/" +
      "?var-ip1=www." + userurl + ".pdw.com" +
      "&var-ip2=www." + userurl + ".plt.com"
  } else {
    // inputString did not match
    // www.something.pdw.com, or
    // www.something.plt.com
  }
}

console.log(convert('www.hello.pdw.com'));
console.log(convert('www.world.plt.com'));
console.log(convert('wrong input format')); // undefined output

Comments

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.