0

I have a url like the following.

http:// myipaddress/folder1/folder2/themes/index.html

Does anyone know of the best approach to always grab whats before and not including /themes/ using javscript string manipulation reg exp techniques. The first bit will regularly be different to the /themes/index.html which is static

5
  • 3
    str.split("/themes")[0] Commented Jul 7, 2014 at 13:28
  • if you sure that "themes/index.html" is static, just replace it with empty string "http:// myipaddress/folder1/folder2/themes/index.html".replace("/themes/index.html", "") Commented Jul 7, 2014 at 13:28
  • @Tommi: that approach presupposes that what follows /themes is always known in advance, which may not be the case. Commented Jul 7, 2014 at 13:30
  • OP said "the /themes/index.html which is static" Commented Jul 7, 2014 at 13:31
  • @tymeJV Why don't you make that an answer? Commented Jul 7, 2014 at 14:01

2 Answers 2

1

You could use lookahead to get all the texts before /themes,

> var str = 'http:// myipaddress/folder1/folder2/themes/index.html';
undefined
> var patt1 = /^.*(?=\/themes)/gi;
undefined
> var result = str.match(patt1);
undefined
> console.log(result);
[ 'http:// myipaddress/folder1/folder2' ]
Sign up to request clarification or add additional context in comments.

Comments

0

you can use javascript split method and remove themes from strings array given by split method and then use join method to concatenate it with your delimeter ('/')

function removeFromStr(str,whatToRemove){
var splitted = str.split('/'),
    filtered = splitted.filter(function(el){
        return el !== whatToRemove;
    });
    return filtered.join('/');
}
test = removeFromStr('http://myipaddress/folder1/folder2/themes/index.html','themes');

console.log(test);

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.