-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>
Community
  • 1
  • 1

1 Answers1

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);
});
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • The regex is working properly. However, it is not appending in my context. I tested it here with no results http://jsfiddle.net/QftHp/ – DirkDiggler Jul 20 '12 at 14:27
  • nvrmind previous comment...I tested the code in my context with no results. http://jsfiddle.net/QftHp/ I took a look at the regex and it doesn't pass my tests. http://fiddle.re/3ghc I could be testing wrong. I'm very new to regex. – DirkDiggler Jul 20 '12 at 14:34
  • 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. – DirkDiggler Jul 20 '12 at 17:29
  • 1
    @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... – Ωmega Jul 20 '12 at 17:35
  • It does work! multiple dev extensions in firefox was causing it to not run javascript. Thank you! My apologies. – DirkDiggler Jul 20 '12 at 18:13