I need to extract a particular string in javascript of format: “STATE: ip reachable” from another set of string I have as input.
From the extracted string, I need to extract both the numbers.
I have done so far is:
var str=<the input value>;
if(str.contains("STATE"))
{
var str = str.substring(str.indexOf("STATE"), string.indexOf("reachable");
}
I am finding difficulty in how to extract the numbers.
I don't really understand what are the 'numbers', but I tried that :
var str= "lorem ipsum STATE: 10.0.5.2 120.30.66.8 test ... blabla bla";
str = str.slice(str.indexOf("STATE"), str.length);
var tab = str.split(" ");
var num1 = tab[1];
var num2 = tab[2];
Use a regular expression...something like this:
var test = "STATE 10.0.0.18 192.168.42.9 reachable";
var ips = test.match(/(\d+\.\d+\.\d+\.\d+)/g);
for (i = 0; i < ips.length; i++) {
document.write(ips[i]);
document.write("<br>");
}
If you are sure about the pattern in the string, you can use below code :
var str = "STATE 10.0.0.18 192.168.42.9 reachable";
if (str.indexOf("STATE") >= 0) {
var str = str.substring(str.indexOf("STATE ")+6, str.indexOf(" reachable"));
var arrIP = str.split(" ");
for (i = 0; i < arrIP.length; i++) {
alert(arrIP[i]);
}
}
jsfiddle : https://jsfiddle.net/nikdtu/4nx50b81/
Related
i need to split a tamil word by character and print it using javascript.
Example :
Input - ஆண்டாள்
output - ஆ
ண்
டா
ள்
can someone help me.
To take each individual character of the string and print it separately you’d want to do:
var myString = "ஆண்டாள்";
var myArray =[];
for(var i = 0; i<myString.length; i++){
myArray.push(myString.charAt(i));
}
//Then print each character however you want example:
for(var i=0; i<myArray.length; i++){
console.log(myArray[i]);
}
If you don't want each character saved in an array you can also do:
var myString = "ஆண்டாள்";
for(var i = 0; i<myString.length; i++){
console.log(myString.charAt(i));
}
Split tamil word by character using javascript...
Input - ஆண்டாள்
Output - ஆ,ண்,டா,ள் (array)... code works 100%
str = "ஆண்டாள்";
var diacritics = {'\u0B82':true,'\u0BBE':true, '\u0BBF':true,
'\u0BC0':true, '\u0BC1':true, '\u0BC2':true, '\u0BC6':true,
'\u0BC7':true, '\u0BC8':true, '\u0BCA':true, '\u0BCB':true,
'\u0BCC':true, '\u0BCD':true, '\u0BD7':true};
var str1 = str.split('');
var Tamil = [];
for(var i = 0; i != str1.length; ++i){
var ch = str1[i];diacritics[ch] ?(Tamil[Tamil.length - 1] +=
ch) : Tamil.push(ch);
}
alert(Tamil);
one more best way is below
str = "ஆண்டாள்";
console.log(str.match(/[\u0b80-\u0bff][\u0bbe-\u0bcd\u0bd7]?/gi));//return ["ஆ", "ண்", "டா", "ள்"]
This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 5 years ago.
Suppose my string is like:
var str = "USA;UK;AUS;NZ"
Now from some a source I am getting one value like:
country.data = "AUS"
Now in this case I want to remove "AUS" from my string.
Can anyone please suggest how to achieve this.
Here is what I have tried:
var someStr= str.substring(0, str.indexOf(country.data))
In this case I got the same result.
var str = "USA;UK;AUS;NZ"
console.log(str + " <- INPUT");
str = str.split(';');
for (let i = 0; i < str.length; i++) {
if (str[i] == 'AUS') {
str.splice(i, 1);
}
}
console.log(str.join(';') + " <- OUTPUT");
You can use split and filter:
var str = "USA;UK;AUS;NZ"
var toBeRemoved = "AUS";
var res = str.split(';').filter(s => s !== toBeRemoved).join(';');
console.log(res);
Try this :
var result = str.replace(country.data + ';','');
Thanks to comments, this should work more efficently :
var tmp = str.replace(country.data ,'');
var result = tmp.replace(';;' ,';');
You can use replace() with a regex containing the searched country, this is how should be the regex /(AUS;?)/.
This is how should be your code:
var str = "USA;UK;AUS;NZ";
var country = "AUS";
var reg = new RegExp("("+country+";?)");
str = str.replace(reg, '');
console.log(str);
This will remove the ; after your country if it exists.
Here is a good old split/join method:
var str = "USA;UK;AUS;NZ;AUS";
var str2 = "AUS";
var str3 = str2 + ";";
console.log(str.split(str3).join("").split(str2).join(""));
So the goal of this task is translate english input values into french and vice versa. The problem here is that I don't know how to split the whole input by spaces to get all the words one by one and translate them one by one. Thank you :)
function translateInput(){
for(i = 0; i < ('input').length; i++){
('input').eq(i).val(('value').eq(i).text());
}
}
var translateText = function() {
var translationType = document.getElementById('translation').value;
if (translationType === 'englishToFrench') {
console.log('translation used: English to French');
return 'code1';
}else if(translationType === 'frenchToEnglish'){
console.log('translation used: French to English');
return 'code2';
}else{
return "No valid translation selected.";
}
};
You can use the split function to split the string at its spaces into an array.
var str = YOUR_STRING;
var array = str.split(" ");
http://www.w3schools.com/jsref/jsref_split.asp
Then you can loop through the array and translate word by word.
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
alert(array[i]);
//Translate string
}
Or you can use a Regular Expression, by the way you can practice in a Regex Playground.
var myString = "Hello, my name is JavaScript";
var tokens = a.match(/\w+'?\w*/g); //Assuming you can take words like {"Bonsanto's", "Asus'"}
tokens.forEach(function(word){
console.log(word);
});
Would somebody here please show me the way to modify the regexp below so that I could then with it get multiple integers per array item? I can detect one integer like in the top-most str below using \d+. However, the function will error out with the other two examples, below it, str = "7yes9 Sir2", etc. Thank you.
//str = "10 2One Number*1*";
//output -> [10, 2One, Number*1*] -> [10 + 2 + 1] -> 13
var str = "7Yes9 Sir2";
//output -> NaN
//var str = "8pop2 1";
//output -> NaN
function NumberAddition(str) {
input = str.split(" ");
var finalAddUp = 0;
var finalArr = [];
for(var i = 0; i<=input.length-1; i++) {
var currentItem = input[i];
var regexp = /(\d+)/g;
finalArr.push(Number(currentItem.match(regexp)));
var itemToBeCounted = +finalArr[i];
finalAddUp += itemToBeCounted;
}
return finalAddUp;
}
console.log(NumberAddition(str));
Try sth. like
HTML
<span id="res"></span>
JS
var str = "7Yes9 Sir2";
var matches = str.match(/\d+/g);
var res=0;
for (var i=0; i< matches.length; i++) {
res += parseInt(matches[i],0);
}
$('#res').html(res);
See this working fiddle
I have a string ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown that will come into a javascript function using sender from my asp.net button control...
<asp:Button ID="buttStartTimer" runat="server" CausesValidation="false" OnClientClick="javascript:countdown(this);" Text="Start" />
function test(sender) {
}
The need to get the number directly following ctrl, In the example above it would be 06 (ctrl06_lblCountDown)
How can I extract this number using javascript?
Thanks
var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown",
result = str.match(/.*ctrl(\d+).*/)[1];
Working example: http://jsfiddle.net/RXGb2/
You can extract is using regex easily:
var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown";
var num = parseInt(str.match(/_ctrl([\d]*)_/)[1], 10);
Safer way:
var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown";
var parts = str.match(/_ctrl([\d]*)_/), num;
if(parts.length > 1) {
num = parseInt(parts[1], 10);
}
You could try something like this:
var str = 'ctrl06_lblCountDown',
numericArray = [],
numericString,
num,
i=0,
len = 0;
numericArray = str.match(/[0-9]/g);
len = numericArray.length;
numericString = '';
for(i=0; i<len; i++){
numericString += numericArray[i];
}
num = parseInt(numericString,10);