Use a regular expression that matches word boundaries, so that local
doesn't match localhost
.
$('script[src$="core.js"]').attr('src').split(/\b(?:core|local)\b/)[0];
質問
I want to get the file root's location by using keywords such as below. But both keywords give me different result.
<script type="text/javascript" src="http://localhost/website/core/core.js"></script>
<script type="text/javascript" src="http://localhost/website/local/ready.js"></script>
$('script[src$="core.js"]').attr('src').split('core')[0];
// get this --> http://localhost/website/
$('script[src$="ready.js"]').attr('src').split('local')[0];
// get this --> http://
I am after http://localhost/website/
as my result.
Is it possible to add regex into the split function such as below?
$('script[src$="core.js"]').attr('src').split('core|local')[0];
解決
Use a regular expression that matches word boundaries, so that local
doesn't match localhost
.
$('script[src$="core.js"]').attr('src').split(/\b(?:core|local)\b/)[0];
他のヒント
You can use:
$('script[src$="core.js"]').attr('src').split(/\/core\/|\/local\//)[0]+'/'
What the above does is:
/core/
or /local/
whichever it finds first/
at the end since that is not part of the first split