I have a string like this - tom|harry|john|elizabeth|hopkin|wayne
I would like to extract the value john and wayne from this string and store in variables. How can I do that in jquery or javascript?
If you know which "index" john and wayne is, you can use split() to make it an array and get it from an array with index.
var str = "tom|harry|john|elizabeth|hopkin|wayne"
var arr = str.split("|");
var third = arr[2],
last = arr[arr.length-1];
Related
Okay, so I have a filepath with a variable prefix...
C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade
... now this path will be different for whatever computer I'm working on...
is there a way to traverse the string up to say 'secc-electron\', and drop it and everything before it while preserving the rest of it? I'm familiar with converting strings to arrays to manipulate elements contained within delimiters, but this is a problem that I have yet to come up with an answer to... would there be some sort of regex solution instead? I'm not that great with regex so I wouldn't know where to begin...
What you probably want is to do a split (with regex or not):
Here's an example:
var paragraph = 'C:\\Users\\susan ivey\\Documents\\VKS Projects\\secc-electron\\src\\views\\main.jade';
var splittedString = paragraph.split("secc-electron"); // returns an array of 2 element containing "C:\\Users\\susan ivey\\Documents\\VKS Projects\\" as the first element and "\\src\\views\\main.jade" as the 2nd element
console.log(splittedString[1]);
You can have a look at this https://www.w3schools.com/jsref/jsref_split.asp to learn more about this function.
With Regex you can do:
var myPath = 'C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade'
var relativePath = myPath.replace(/.*(?=secc-electron)/, '');
The Regex is:
.*(?=secc-electron)
It matches any characters up to 'secc-electron'. When calling replace it will return the last part of the path.
You can split the string at a certain point, then return the second part of the resulting array:
var string = "C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade"
console.log('string is: ', string)
var newArray = string.split("secc-electron")
console.log('newArray is: ', newArray)
console.log('newArray[1] is: ', newArray[1])
Alternatively you could use path.parse(path); https://nodejs.org/api/path.html#path_path_parse_path and retrieve the parts that you are interested in from the object that gets returned.
I'm having string like this
String input = "ABCD|opt/kelly/box.txt|DXC|20-12-2015 11:00:00"
I have tried lot of options by google-ing like indexOf() over load etc but could not get the exact result.
Is that possible I could have multiple output string on the basis of "|"
Expected output
String one = input.substring(0,input.indexOf("|")) = ABCD
String two = opt/kelly/box.txt
String three = DXC
String four = 20-12-2015 11:00:00
How can I do for the remaining ones ?
Any suggestion please how can I get this result using indexOf with substring.
Thanks in Advance !!
It's easy. All you need to do is to use .split:
var input = "ABCD|opt/kelly/box.txt|DXC|20-12-2015 11:00:00";
input = input.split("|");
console.log(input);
But if you need them in variables like one, two, etc., you might need to use destructuring assignment. You don't need to use .indexOf here.
Using Destructuring assignment
var input = "ABCD|opt/kelly/box.txt|DXC|20-12-2015 11:00:00";
var [one, two, three, four] = input.split("|");
console.log(one);
console.log(two);
console.log(three);
console.log(four);
First, be aware that JavaScript doesn't allow you to declare your data type as you are doing with:
String input ....
You can only declare the variable (i.e. var input ...)
Barring that, the .split() method (which splits a string based on your delimiter and returns an array of the parts to you) will do it.
Also, if you need to store each array element in its own variable, you can use a destructuring assignment to accomplish that.
// Here's your scenario:
var input = "ABCD|opt/kelly/box.txt|DXC|20-12-2015 11:00:00";
var one = input.substring(0,input.indexOf("|")) // ABCD
// Do the remaining split on the original string without the already found parts
var [two, three, four] = input.replace(one + "|","").split("|");
console.log(one);
console.log(two);
console.log(three);
console.log(four);
// Here'e a cleaner alternative that uses a destructuring assignment:
var input2 = "ABCD|opt/kelly/box.txt|DXC|20-12-2015 11:00:00";
var [one2, two2, three2, four2] = input.split("|");
console.log(one2);
console.log(two2);
console.log(three2);
console.log(four2);
I have two GUIDs. I am looking for to replace c013d94e from 1st guid with cd11d94e of second guid in Javascipt.
I checked javascript replace() method but not sure how i can use it with my specific case.
c013d94e-3210-e511-82ec-303a64efb676 - 1st Guid
cd11d94e-3210-e511-82ec-303a64efb676 - 2nd Guid
Following is my code where i am trying to do it
for(var i=0; i < response[1].length;i++)
angular.forEach($scope.studentPermissions[i][0].Children, function (subject) {
string 1stGuid= response[1].data[i].Id; // it contains cd11d94e-3210-e511-82ec-303a64efb676
subject.Id = // it contains c013d94e-3210-e511-82ec-303a64efb676
});
replace takes 2 parameters, the first is the string to search for and the second is the replacement string. It doesn't modify the original string, it simply returns a new string with the value replaced.
You can perform your replace like this:
var guid = 'c013d94e-3210-e511-82ec-303a64efb676';
guid = guid.replace('c013d94e', 'cd11d94e');
console.log(guid); // 'cd11d94e-3210-e511-82ec-303a64efb676'
#Jamen. Yes the other part of 1st string will always be same. How can i use concatenate?
You don't even need to use replace then? Just make a brand new string:
var guid = "cd11d94e-3210-e511-82ec-303a64efb676";
But, to actually answer the question in the title:
var input = "c013d94e-3210-e511-82ec-303a64efb676";
var output = input.replace("c013d94e", "cd11d94e");
console.log(output); // cd11d94e-3210-e511-82ec-303a64efb676
But like I said, in your situation this shouldn't be necessary, based on the quote.
I have a form that user fill with tags like this:
<messageCode>Tag Value 1</messageCode>
<messageVersion>Tag Value 2</messageVersion>
And would like to know if there is a way in Javascript to search the following strings:
String 1 = <messsageCode>
String 2 = Tag Value 1?
I should use index of?
If you insist on parsing xml with regexps and string functions, then
input.match(/<(.*?)>(.*)<\/\1>/)
will return an array, whose [1] element is the tagname and [2] element the content.
Instead, you should use DOMParser to reliably parse the input, and use DOM functions to navigate the result.
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
this will only tell you the start position of welcome. Do you want to retrieve the value?
var str = "tag attr = tag value";
var n = str.indexOf("=");
var res = str.substring(n,);
i have this static code that will hide/show some combo box but what if i add more category´s ?well if do add more category´s i have to change the code every time
so what i want to do is have a var that will receive several values separated by comma
and them some how it will separate the values and them it will store the values in a array. and now when the user needs to add more category´s i don't have to edit the code.
but how can i separate the values divided by a comma and then add them to a array?
If I understand your question correctly you'll want to have a look at http://www.w3schools.com/jsref/jsref_split.asp
And use var arr = YOURVARIABLE.split(',');
You can use the String.split() function, e.g:
var s = '1,2,3,4,5,6';
var values = s.split(',');
console.log(values);
For more information see here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split
You would use the split() method. http://www.w3schools.com/jsref/jsref_split.asp
var string = '1,2,3,text,123';
var array = string.split(',');
//array = [1, 2, 3, 'text', 123];
Try the following code
var testString = "comma,seperated,list";
var stringArr = testString.split(",");
the split() method will literally split the string by the delimeter passed to it and return an array of values. in this case our array will be
// stringArr = ["comma", "seperated", "list"];