my code
var str =$(this).attr('id');
this will give me value == myid 5
var str1 = myid
var str2 = 5
i want something like this ..
how to achieve this using split method
var str =$(this).attr('id');
var ret = str.split(" ");
var str1 = ret[0];
var str2 = ret[1];
Use in-built function: split()
var source = 'myid 5';
//reduce multiple places to single space and then split
var splittedSource = source.replace(/\s{2,}/g, ' ').split(' ');
console.log(splittedSource);
Note: this works even there is multiple spaces between the string groups
Fiddle: http://jsfiddle.net/QNSyr/6/
One line solution:
//<div id="mypost-5">
var postId = this.id.split('mypost-')[1] ); //better solution than the below one!
-OR-
//<div id="mypost-5">
var postId = $(this).attr('id').split('mypost-')[1];
Related
This is string
chat('star_comment','11211967','17','HF00008','MLR51101639100ICI100','B19','0','2020');
i want to replace it to like this in javascript:
chat('star_comment','11211967','17','HF00008','MLR51101639100ICI100','B19','RO_RX','2020');
my code is like this:
var str = "chat('star_comment','11211967','17','HF00008','MLR51101639100ICI100','B19','0','2020')";
check = "0";
var res = str.replace("/\/"+check+"\//", "RO_RX");
but this is not working
Simplify the replacement and put the single quotes in the selector (check) direcctly
var str = "chat('star_comment','11211967','17','HF00008','MLR51101639100ICI100','B19','0','2020')";
let check = "'0'";
var res = str.replace(check, "'RO_RX'");
console.log(res);
I am new with RegEx, but it would be very useful to use it for my project. What I want to do in Javascript is this :
I have this kind of string "/this/is/an/example" and I would like to extract each word of that string, that is to say :
"/this/is/an/example" -> this, is, an, example. And then use each word.
Up to now, I did :
var str = "/this/is/a/test";
var patt1 = /\/*/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
and it returns me : /,,,,,/,,,/,,/,,,,,
I know that I will have to use .slice function next if I can identify the position of each "/" by using search for instance but using search it only returns me the index of the first "/" that is to say in this case 0.
I cannot find out.
Any Idea ?
Thanks in advance !
Use split()
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
var str = "/this/is/a/test";
var array = str.split('/');
console.log(array);
In case you want to do with regex.
var str = "/this/is/a/test";
var patt1 = /(\w+)/g;
var result = str.match(patt1)
console.log(result);
Well I guess it depends on your definition of 'word', there is a 'word character' match which might be what you want:
var patt1 = /(\w+)/g;
Here is a working example of the regex
Full JS example:
var str = "/this/is/a/test";
var patt1 = /(\w+)/g;
var match = str.match(patt1);
var output = match.join(", ");
console.log(output);
You can use this regex: /\b[^\d\W]+\b/g, to have a specific word just access the index in the array. e.g result[0] == this
var str = "/this/is/a/test";
var patt1 = /\b[^\d\W]+\b/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
<span id="demo"></span>
I've a strings with the values like:
share__43
share__153
share
share_section
How do I get the integer values like 43 or 153?
Try this
var regex = new RegExp(/([0-9]+)/g);
var test = "share__43 share__153 share share_section";
var match = regex.exec(test);
alert('Found: ' + match[1]);
Fiddle
Example, currently using a single string
var regex = /\d+/;
var str = "share__43";
alert (str.match(regex ));
Demo
I have a JavaScript string sentrptg2c#appqueue#sentrptg2c#vwemployees#
I want to get last part of the string: vwemployees through RegExp or from any JavaScript function.
and also want to remove that last keyword from string so that next time string will be like this sentrptg2c#appqueue#sentrptg2c#
I have tried
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var array = url.split('/');
var lastsegment = array[array.length-1];
and get vwemployees last segment but the string remains the same
sentrptg2c#appqueue#sentrptg2c#vwemployees#
It should be sentrptg2c#appqueue#sentrptg2c# when above code runs.
Please suggest a way to do this in JavaScript
JSFiddle: http://jsfiddle.net/satpalsingh/ykBCG/
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
//Assuming # as seperator
var array = str.split('#');
//Clear empty value in array
var newArray = array.filter(function(v){return v!==''});
var lastsegment = newArray[newArray.length-1];
alert(lastsegment);
//output is "vwemployees"
<script type="text/javascript">
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var arr = str.split("#");
alert(arr[arr.length-2]);
</script>
split function will do the job for you.
use split() , splice() to remove from array and join() to join them back again
var str="sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var reqvalue=str.split('#');
alert(reqvalue[3]);
reqvalue.splice(3,1);
alert(reqvalue.join('#'))
fiddle here
The split function can help you: http://www.w3schools.com/jsref/jsref_split.asp
yourString.split("#");
You will get an array with your values: sentrptg2c,appqueue,sentrptg2c,vwemployees
After that you just have to remove the last element, and rebuild your string from this array.
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
alert(str);
var arr = str.split('#');
var lastsegment = arr[arr.length-2];
alert(lastsegment);
var new_str = str.replace(lastsegment+'#', '');
alert(new_str);
How to replace comma by double quote in javascript?
For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"
str = '"c,C#,JavaScript"';
str = str.split(',').join('","');
That would result in "c","C#","JavaScript"
var original = '"c,C#,JavaScript"';
var quoted = original.replace(/,/g, '","'); // "c","C#","JavaScript"
Just to toss it in there, you could also .split() and .join(), like this:
var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');
You can test it here.
You can do this:
str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');
Result:
c"C#"JavaScript