use javascript split function twice - javascript

I have the following string:
12.1.20.1, 81.32.68.68:50321
however I need to make it like this:
81.32.68.68
Could someone help me out with this?
I've already tried to use split(",")[1] but I also need to remove the :50321
Thank you!

If you can use the split function twice on it, you can do something like this:
console.log(
`12.1.20.1, 81.32.68.68:50321`
.split(", ")[1]
.split(":")[0]
);
This gives just the IP address out.
81.32.68.68

Related

filter a part of an url that goes like image-123-123 regex

I need to get the generated size of an image in wordpress using Javascript, the url looks like this:
wp-content/uploads/2018/04/image-300x200.jpg
and i need to get only the value
300x200
i tried a few regex i found but being new to REGEX i dont seem able to make it work.
Simply do \d+x\d+ or:
\d+x\d+(?=\.jpg)
This uses a look ahead (?=) to match every \d+x\d+ when it appears before .jpg
use this:
/(\d+?x\d+?)\./
according to format NumbersxNumbers.
console.log('wp-content/uploads/2018/04/image-300x200.jpg'.match(/(\d+?x\d+?)\./)[1])

how do I create a page name from the location.pathname url?

I have the following url :
http://localhost/get/this/url/in/a-nice-format.html
var pagename= location.pathname.split('/').join(':');
gives me
:get:this:url:in:a-nice-format.html
How do I get rid of the first ":"?
Thanks.
pagename = location.pathname.split('/').join(':').substr(1);
Hi you can use substrings method. Simply do this....
var pageName2 = pageName.substring(1); I hope this helps.
I'm not able to comment yet but waned to help anyway. And there's probably an answer somewhere else...
Anyway, try using slice() and passing in a 'begin' argument of 1 to remove the first ':'.
Like this: var pagename= location.pathname.split('/').join(':').slice(1);

How to combine many comparable find query in javascript

I made this script in javascript, it's work:
if(datatermid==16){
$('#Xhaut').find('[data-parent=16]').removeClass('active')
}
if(datatermid==17){
$('#Xhaut').find('[data-parent=17]').removeClass('active')
}
if(datatermid==18){
$('#Xhaut').find('[data-parent=18]').removeClass('active')
}
I thinks it's possible to combine all this lines into one , maybe somethink like :
$('#Xhaut').find('[data-parent=datatermid]').removeClass('active')
But it dont work...
can you please give me a solution?
Regards
Concatenate the variable using the + operator to build your selector :
$('#Xhaut').find('[data-parent='+datatermid+']').removeClass('active')
You may also do the search in one step :
$('#Xhaut [data-parent='+datatermid+']').removeClass('active')

JavaScript, get url body in a variable

I know it is an easy one, sorry for asking, but I’m going mad and I cannot get it. Must be the fundamentals, I’ve got them all mix up :((
I have this url:
https://www.myweb.sub.com/~user/folder/file.php?param=Value
I need to get:
https://www.myweb.sub.com/~user/folder/
in a variable.
Please help to achieve it.
Thanks a lot
You can use the slice() and lastIndexOf() methods:
var url = "https://www.myweb.sub.com/~user/folder/file.php?param=Value";
var segment = url.slice(0, url.lastIndexOf("/") + 1);
If your current URL is actually https://www.myweb.sub.com/~user/folder/file.php?param=Value, operate on window.location.href instead.
So we don't chew it all up for you, here's a link that should set you on the right path:
find the last index of a character with this: http://www.w3schools.com/jsref/jsref_lastindexof.asp
get the substring of a string with this:
http://www.w3schools.com/jsref/jsref_substring.asp
Tell us if you still need help after that.
use this :-
var str="https://www.myweb.sub.com/~user/folder/file.php?param=Value";
var n=str.substring(0,str.indexOf("file"));

Javascript regex replace between closing and opening tags

I realise this must be a really easy piece of regex, but just can't seem to work it out. I just need to search a string for this:
</p><p>
And add a comma between them, like this:
</p>,<p>
Ignore the fact it isn't nice html, it makes sense to me though!
I tried this, but didn't seem to work:
str.replace(/<\/p><p>/g, "</p>,<p>");
Any ideas? Thanks :)
I tried this, but didn't seem to work:
str.replace(/<\/p><p>/g, "</p>,<p>");
replace returns a new string with the result, it doesn't modify the string you called it on. So that would be:
str = str.replace(/<\/p><p>/g, "</p>,<p>");
// ^^^^^^
This works for me:
alert("Some </p><p> to replace".replace(/<\/p><p>/g, "</p>,<p>"));
Your code works just fine: http://jsfiddle.net/eBkhR/
Strings in javascript are immutable. That is, the contents in a string cannot be modified. Therefore, when using replace method it cannot replace the content but it just returns a new string with the new contents. You would need to store the new string the in required variable.
for example,
str = str.replace(/<\/p><p>/g, "</p>,<p>");
The answers with alert will work because the new string is getting passed to the alert function. The string itself is not modified.
My fault. It does work. I'd forgotten that I have ids in each p tag, so just needed to search for this:
/<\/p><p/g
Thanks for all the replies though!

Categories