I stuck again in beginner-spheres of JS. :/
I want to get the number from data-channel-id, which is found by the call in variable spans:
<main class="soft--top" data-channel-id="348" data-preview-mode="">
This is my JS:
window.onload = function (){
var spans = document.getElementsByTagName('main')[0];
var res = /[0-9]+/;
var match = res.exec(spans);
console.log(match);
}
I don't know how to proceed.
The Regex is correct, the document. variable "spans" shows the correct result, but matching won't show any output in the console.
What am I missing?
spans in your code currently contains the whole element.
To use only the data-channel-id value, see the code below:
var spans = document.getElementsByTagName('main')[0].getAttribute('data-channel-id');
spans is not string but HTMLElement, to get value of data-channel-id you can use getAttribute as mentioned in comment above or if you really want to use regex convert it to string with .outerHTML
window.onload = function() {
var spans = document.getElementsByTagName('main')[0];
var res = /[0-9]+/;
var match = res.exec(spans.outerHTML);
console.log(match);
}
I want to get the number from "data-channel-id"
No need to use regex. Just:
var spans = document.getElementsByTagName('main')[0];
var match = spans.getAttribute('data-channel-id');
console.log(match);
Working demo here.
Related
I want to .split(" ") a data attribute value but it returns empty:
var bulletcontent = jQuery(this).closest("div").find("p").attr('data-bcontent');
// split content into words
var words = jQuery(bulletcontent).text().split(" ");
Why isn't this working?
Here is a jsFiddle.
See the fiddle: https://jsfiddle.net/43439o8o/2/
You dont need to get the .text(), the .attr() already return his content:
var bulletcontent = jQuery(content).attr('data-bcontent');
// split content into words
var words = bulletcontent.split(" "); //Remove the .text() method
As you can see at the .attr() method docs, you can use it to return or set the attribute value:
Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
change this line:
var words = bulletcontent.split(" ");
$(document).ready(function() {
$(document).on("click", ".click", function() {
// find the bullet content stored in p tag inside li
var content = $(this).prev("p").attr('data-bcontent');//get the p attr
alert(content)
var words = content.split(' ')//split
alert(words[0]);//alert first value
alert(words[1]);//alert second value
});
});
DEMO
You can use directly function split() on the string:
var words = bulletcontent.split(" ");
for example you can see this link
.split() is a Method of String in Javascipt so there's no need to use jQuery. just add .split(' ') right next to your variable(String).
.split() returns array of strings depends on how you want to plit the original string.
This should work for you.
var words = bulletcontent.split(" ");
You can use this:
var words = bulletcontent.split(" ");
I have a string like
/abc/def/hij/lmn.o // just a raw string for example dont know what would be the content
I want only /abc/def/hij part of string how do I do that.
I tried using .split() but did not get any solution.
If you want to remove the particular string /lmn.o, you can use replace function, like this
console.log(data.replace("/lmn.o", ""));
# /abc/def/hij
If you want to remove the last part after the /, you can do this
console.log("/" + data.split("/").slice(1, -1).join("/"));
# /abc/def/hij
you can do
var str = "/abc/def/hij/lmn.o";
var dirname = str.replace(/\/[^/]+$/, "");
Alternatively:
var dirname = str.split("/").slice(0, -1).join("/");
See the benchmarks
Using javascript
var x = '/abc/def/hij/lmn.o';
var y = x.substring(0,x.lastIndexOf("/"));
console.log(y);
var s= "/abc/def/hij/lmn.o"
var arr= s.split("/");
after this, use
arr.pop();
to remove the last content of the array which would be lmn.o, after which you can use
var new_s= arr.join("/");
to get /abc/def/hij
I want to remove the last parameter in the href of a specific tag using jquery
for example replace href="app/controller/action/05/04/2014"
by href="app/controller/action/05/04"
Try using the String.lastIndexOf() and String.substring() in this context to achieve what you want,
var xText ="app/controller/action/05/04/2014";
xText = xText.substring(0,xText.lastIndexOf('/'));
DEMO
if you know which value you need to change ,than you can use replace:
var str = "app/controller/action/05/04/2014";
var res = str.replace("2014","04");
or else you can use array and change / update last value in array:
var str = "app/controller/action/05/04/2014";
var res = str.split("/");
We can use a regular expression replace which will be the fastest, compared to substring/slice.
var hreftxt ="app/controller/action/05/04/2014";
hreftxt = hreftxt.replace(/\/[^\/]*$/,"");
i have a string which has the following format (3,1,Mayer,Jack). I need all 4 elements as own variables. I thought about using JavaScript's match()-function to do this, but i don't know how i can get the 4 elements in variables...
I tried it with this code:
var clickedId = '(3,1,Mayer,Jack)';
var pregId = clickedId.match(/([\d]+,[\d]+,[\w]+,[\w]+)/g);
alert(pregId[0]);
But i get a popup windows with "(3,1,Mayer,Jack)"... I thought about something like this:
var id = pregId[0];
var teamid = pregId[1];
var surname = pregId[2];
var name = pregId[3];
How can i do this?
Thanks!
The match function is matching the whole expression, not pulling out the parts of the whole. So you need to match on each individual element, say with a regex like (\d|\w)+. Here's an example:
var clickedId = '(3,1,Mayer,Jack)';
var pregId = clickedId.match(/((\d|\w)+)/g);
var id = pregId[0];
var teamid = pregId[1];
var surname = pregId[2];
var name = pregId[3];
alert(surname);
Demo: http://jsfiddle.net/Znu7N/
Use individual groups in your regex:
clickedId.match(/([\d]+),([\d]+),([\w]+),([\w]+)/g);
Then the rest should work
Instead your put everything into a single group making the whole expression matched as a single string
If I have a string... abcdefghi
and I want to use regex to load every elemnent into an array but I want to be able to stick anything connected by plus sign into the same element... how to do that?
var mystring = "abc+d+efghi"
output array ["a","b","cde","f","g","h","i"]
One way to do it:
var re = /([^+])(?:\+[^+])*/g;
var str = 'abcd+e+fghi';
var a = str.match(re).map(function (s) { return s.replace(/\+/g, ''); });
console.log(a);
The value of a[3] should now be 'def'.
http://jsfiddle.net/rbFwR/2
You can use this expression, to produce [a][b][c+d+e][f][g][h][i].
mystring.split ("(.\+)*.")
Next, replace any + characters with empty on the resulting list.
mystring.split("\\+")
Click here for more information.