-1

Previous stackoverflow

This jquery statement will look for domain.com and append ?parameter to the end of the URL. It will NOT append if ?parameter has already been added.

The problem: My current jquery modifies all URLs and not domain.com. Here is the regex statement that i would like to use and is tested to work. However, when implemented, nothing is appended. Any help is greatly appreciated!

Regex i would like to use:

\b(https?://)?([a-z0-9-]+\.)*domain\.com(/[^\s]*)?

RegexFiddle

JSFiddle for convience

Code to be modified

<div id="wp-content-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content">&lt;a title="Link to test domain" href="http://www.domain.com"&gt;Link to google&lt;/a&gt;
&lt;a href="http://www.google.com/directory/subdirectory/index.html"&gt;This is another link&lt;/a&gt;
&lt;a href="http://domain.com/directory/index.html"&gt;This is a 3rd link&lt;/a&gt;

&lt;a href="http://www.domain.com/subdir?parameter"&gt;this url already has parameters&lt;/a&gt;</textarea></div>

current jquery statement

var url = 'www.domain.com';
var append = '?parameter';

$(".wp-editor-area").each(function() {
    $(this).text(urlify($(this).text()));
});

function urlify(text) {
    var urlRegex = /(\b(https?|ftp|file):\/\/[www.domain.com][-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(urlRegex, function(url) {
        // if the url does not contain append, concat append to the URL
        if (url.indexOf(append) == -1) {
            return url + append;
        }
        return url;
    });

}

Current output

    <a title="Link to test domain" href="http://www.domain.com?parameter">Link to google</a>
<a href="http://www.google.com/directory/subdirectory/index.html?parameter">This is another link</a>
<a href="http://domain.com/directory/index.html?parameter">This is a 3rd link</a>
2
  • Resume: in your current output is the 3rd one fine? Commented Jul 19, 2012 at 22:10
  • the third line in the output is there to test that a duplicate ?parameter doesn't get appended again. Commented Jul 19, 2012 at 22:29

1 Answer 1

2

Test this code - it should be what you need (or at least starting point) >>

function urlify(text) {
  var append = '?parameter';
  text = text.replace(/("(?:(?:https?|ftp|file):\/\/)?(?:www.|)domain.com(?:\/[-a-z\d_.]+)*)(\?[^"]*|)(")/ig,
    function(m, m1, m2, m3) {
      return ((m1.length != 0) && (m2.length == 0)) ? m1 + append + m3 : m;
    });
  return text;
}

$(".wp-editor-area").each(function() {
  this.innerHTML = urlify(this.innerHTML);
});
Sign up to request clarification or add additional context in comments.

5 Comments

The regex is working properly. However, it is not appending in my context. I tested it here with no results jsfiddle.net/QftHp
nvrmind previous comment...I tested the code in my context with no results. jsfiddle.net/QftHp I took a look at the regex and it doesn't pass my tests. fiddle.re/3ghc I could be testing wrong. I'm very new to regex.
Thank you!very very close...it works in Jsfiddle tests..however when actually wrapped in javascript as used as a bookmarklet(intended purpose). It doesn't append anything. However, I don't receive errors. The bookmarklet will essentially modify the code in the div(wp-editor-area) which is a WYSIWIG editing interface in wordpress. My code in the OG question did that, but changed all URLs. I think it has to do with how the function is called.
@user1512806 - If it works with jsfiddle, then it is okay. When I checked your fiddle, there were sooooo many syntax and logical errors, that I am not surprised that when you made some copy/paste with modification, you are out of luck. My answer is a right solution, not a close or very close. You just have to check your code for errors that are on your end...
It does work! multiple dev extensions in firefox was causing it to not run javascript. Thank you! My apologies.

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.