how to parse string - javascript

im trying to parse the following string;
"data[Product][0][fieldname]"
i need to be able to change the "[0]" part to a number, the "data[Product]" wont change while the "[fieldname]" will change.
im not sure how to do it with javascript but have an rough idea.
all i have which is wrong, is below but since [0] varies each time it doesnt work;
name="data[Product][0][fieldname]";
name.replace('[0]', '['+newrowid+']'
Jsfiddle;
http://jsfiddle.net/fuzzy_dunlop/8WKcs/

Use a regexp:
var a = "data[Product][0][fieldname]";
var re = /(.*\[.*\]\[)(.*)(\]\[.*\])/gi;
newstr = a.replace(re, "$1" + "YourString" + "$3");
document.write("Now: " + newstr);
Example code: http://jsfiddle.net/NKbd9/1/

Use this instead
name = name.replace('[0]', '['+newrowid+']');

Replace can take a regular expression instead of a string.
Try this:
name="data[Product][0][fieldname]";
name.replace(/\[\d\]/, '['+newrowid+']');

I assume you are trying to do this inside a loop, in that case you just need to make it dynamic. Also not that replace doesn't change the current string, it returns a new string so you need to assign the value returned back to name.
var i = 0,
len = 10,
name,
newrowid = 13;
for(; i <len; i++) {
name = "data[Product]["+i+"][fieldname]";
name = name.replace('['+i+']', '['+newrowid+']');
}

Related

How to get part of string using javascript?

I have an array with one element
ClickMeArray[0] = "ClickMe=a-6,a-7,a-8,a-9,"
I want variable to return elements after ClickMe= from ClickMeArray.
Output should be a-6,a-7,a-8,a-9,.
ClickMeArray[0].split('ClickMe=')[1];
Try This
var val = "ClickMe=a-6,a-7,a-8,a-9,";
var myString = val.substr(val.indexOf("=") + 1);
There are many way to do this work. One way is using .replace(). You can replace ClickMe= with empty to getting another part of string.
var ClickMeArray = "ClickMe=a-6,a-7,a-8,a-9,";
console.log(ClickMeArray.replace('ClickMe=',''));

Trim a variable's value until it reaches to a certain character

so my idea is like this..
var songList = ["1. somesong.mid","13. abcdef.mid","153. acde.mid"];
var newString = myString.substr(4); // i want this to dynamically trim the numbers till it has reached the .
// but i wanted the 1. 13. 153. and so on removed.
// i have more value's in my array with different 'numbers' in the beginning
so im having trouble with this can anyone help me find a more simple solution which dynamically chop's down the first character's till the '.' ?
You can do something like
var songList = ["1. somesong.mid","13. abcdef.mid","153. acde.mid"];
songList.forEach(function(value, i){
songList[i] = value.replace(/\d+\./, ''); //value.substr(value.indexOf('.') + 1)
});
Demo: Fiddle
You can use the string .match() method to extract the part up to and including the first . as follows:
var newString = myString.match(/[^.]*./)[0];
That assumes that there will be a match. If you need to allow for no match occurring then perhaps:
var newString = (myString.match(/[^.]*./) || [myString])[0];
If you're saying you want to remove the numbers and keep the rest of the string, then a simple .replace() will do it:
var newString = myString.replace(/^[^.]*. */, "");

Select first complete string matching partial string

I have a page which contains many tags with a string in them, for example 'Am I a String? Yes'.
Using JQuery, I want to get the first instance of 'Am I a String? ' and then take the Yes or No after that and store it so I can use it in a conditional.
Any ideas?
Thanks.
The code I'm going with:
function experimentThree() {
var stringBool = $('*:contains("Was it enough? ")');
console.log('stringBool = : ' + stringBool);
console.log('stringBool Object 1 = : ' + stringBool[0]);
}
Thinking if I can get the complete string in the first object I can compare that to what I expect to see.
check this out
$('body').find('*:contains("Am I a String")').each(function(index, crntNode){
var parts = $(crntNode).text().split('?');
var flag = parts[1].trim();
alert(flag);
});
for example:
var a = $('div').text().match(/Am I a String\? (Yes|No)/g)[0];
var b = a.match(/(Yes|No)/g)[0];

How can I find a specific string and replace based on character?

I'm trying to find a specific character, for example '?' and then remove all text behind the char until I hit a whitespace.
So that:
var string = '?What is going on here?';
Then the new string would be: 'is going on here';
I have been using this:
var mod_content = content.substring(content.indexOf(' ') + 1);
But this is not valid anymore, since the specific string also can be in the middle of a string also.
I haven't really tried anything but this. I have no idea at all how to do it.
use:
string = string.replace(/\?\S*\s+/g, '');
Update:
If want to remove the last ? too, then use
string = string.replace(/\?\S*\s*/g, '');
var firstBit = str.split("?");
var bityouWant = firstBit.substring(firstBit.indexOf(' ') + 1);

Any javascript string function?

Some outside code is giving me a string value like..
null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390,
now i have to save this value to the data base but putting 0 in place of null in javascript. Is there any javascript string releated function to do this conversion?
You can simply use the replace function over and over again until all instances are replaced, but make sure that all your string will ever contain is the character sequence null or a number (and obviously the delimiting comma):
var str = "null,402,2912,null"
var index = str.indexOf("null");
while(index != -1) {
str = str.replace("null", "0");
index = str.indexOf("null");
}
You need to run a for loop because the function String.replace(search, rplc) will replace only the first instance of search with rplc. So we use the indexOf method to check, in each iteration, if the required term exists or not. Another alternative (and in my opinion, a better alternative would be:
var str = "null,402,2912,null"
var parts = str.split(",");
var data = []
for(var i=0; i<parts.length; i++) {
data[data.length] = parts[i]=="null"?0:parseInt(parts[i]);
}
Basically, what we are doing is that since you will anyways be converting this to an array of numbers (I presume, and sort of hope), we first split it into individual elements and then inspect each element to see if it is null and make the conversion accordingly.
This should answer your needs:
var str = 'null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390';
str.split(",").map(function (n) { var num = Number(n); return isNaN(num) ? 0 : num; });
The simplest solution is:
var inputString = new String("null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390,");
var outputString = inputString.replace("null", "0");
What I understood from your question is:
You want to replace null with 0 in a string.
You may use
string = "null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390,"
string.replace(/null/g,0)
Hope it helps.

Categories

Resources