user3036965
user3036965

Reputation: 155

Javascript String Manipulation getting part of a string

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

Upvotes: 0

Views: 58

Answers (2)

Narek Mamikonyan
Narek Mamikonyan

Reputation: 4611

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);

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

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' ]

Upvotes: 1

Related Questions