How to get part of string using javascript? - 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=',''));

Related

How To Remove All Text Before a Specific Value in a String

I have the following string, "blahblahhellothere", that I would like to be shortened to "hellothere" using JavaScript and/or JQuery.
I have tried using the following code:
var titletext123 = "blahblah<br>hellothere"
var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") +1);
Which only returns "br>hellothere".
Does anyone have any code which will get rid of the and all text before it?
Thank you very much. All of your help is appreciated!
Make it
var titletext123 = "blahblah<br>hellothere" var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") + 4);
So it is +4. Which accounts for all the characters in <br>.
You can use split() and get second element.
var titletext123 = "blahblah<br>hellothere" ;
var injuryt3xt = titletext123.split("<br>")[1];
alert(injuryt3xt);
Using regular expression:
var text = "blahblah<br>hellothere"
var clipped = str.replace(/.+\<br\>/, ""));
Another option (depending on circumstances) might be:
var injuryt3xt = titletext123.split("<br>")[1];
Which would split the string on <br> and return an array with the left-over parts ... the second of which is referred to with the [1]

Javascript String replace "'"

How do I replace ' in javascript. For example I want to convert O'conor to O-conor.
This doesnt work. I am doing something stupid.
var dummyStr = "O'conor";
dummyStr.replace("'","-");
console.log(dummyStr); //prints O'conor
dummyStr.replace(/'/g,"-"); //still prints O'conor not O-conor
Please mark duplicate if this has already been asked elsewhere.
replace (cf. replace on W3Schools) does not modify the current string. You have to assign it like this :
dummyStr = dummyStr.replace("'","-");
console.log(dummyStr); //prints O-conor
You need to assign a variable to the return value of replace()
e.g.
var dummyStr = "O'conor";
var ammendedString = dummyStr.replace("'","-");
console.log(ammendedString );
you just need to store this to some variable after replace, like below
dummyStr = dummyStr.replace("'","-");
dummyStr = dummyStr.replace("'","-");
Btw for replacing all:
Replace All - StackOverFlow

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 to parse string

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+']');
}

selecting second string point using js substring

i want to select a sting from the long para. it has number of dot('.')s. i want to trim the word from the second one, is it any way to do this?
example
var name = "one.two.three";
name.substring(0,name.indexOf('.'))
name.substring(0,name.lastIndexOf('.'))
from above trimming in case if i use indexOf it gives first word (one), if i use lastIndex of it gives the word (three), but i need to select the second one, to get value as 'second'
how can i trim this using indexOf method? or to select multicombination strings like one.three or one.two, or two.three?
thanks in advance!
use string.split.
e.g.
name.split(".")[1]
var name="one.two.three";
var result=name.split(".").slice(0,2).join(".");
Example:
"".split(".").slice(0,2).join(".") // return ""
"one".split(".").slice(0,2).join(".") // return "one"
"one.two".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three.four.five".split(".").slice(0,2).join(".") // return "one.two"
Is that work for you ?
var name = "one.two.three";
var params = name.split('.');
console.log(params[1]);
use Split
var name = "one.two.three";
var output = name.split('.');
alert(output[1]);
example here

Categories

Resources