JSFiddle - React, Tailwind, and code Playground

HTML

<script>
    function test() {
    var text = 'Some text not nice www.org/?www.com and www.org/e?www.com  very nice! and domains www.dom1.com?www.dom2.net and text and ';
    
    // uncomment to test www.dom2.net as cgi parameter
    // in this case only one A tag must in result
    // text = 'www.dom1.com/bin?www.dom2.net';
    
    text = text + '?www.dom1.com/bin?website.dom2.net and text with www.domain.net again at the end: www.example.com/cgi-bin/printenv.pl/foo/bar?var1=value1&var2=with%20percent%20encoding END www.google.ru/webhp?hl=en&tab=ww#fp=744271a7ece6d6d7&hl=en&newwindow=1&q=javascript+replace+function END and www.msdn.microsoft.com/en-us/library/windows/desktop/ms683180(v=vs.85).aspx DONE and if we put dot at the end of url, it must not be included! www.domain.com. As in text or at end of stirng like www.nettt.org.';

    // so, i think, need detect end of URL with '?' only if no one '/' in that url
    // and pass ? if it used as url (not really question)

    var r = /(^|[\?\s])(www\.[^\? ]+\/[^\/ ]*\?[^\? ]+|www\.[^\? ]+)/g;

    
        var res = text.replace(r, '$1<a href="$2">$2</a>');

        document.getElementById('b').innerHTML = res;
    }
</script>
<div id="b">...</div>
<br>
<div onclick="test();">click</div>