Javascript match - javascript

Suppose this is my code
var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;";
var patt1=/abc=([\d]+)/g;
document.write(str.match(patt1));
i want the output as 1234587,19855284
this doesnt return the number but instead returns the complete string which is in the pattern
if i remove 'g' from the pattern it returns abcd=1234578,1234578 what am i doing wrong??

match() returns an array. The first entry (index 0) is always the matching string. Following that you get the matching group(s).
The toString()-logic of an array takes all elements and joins them with ", ". You can use e.g. join("-") to change that.

Try following code.
var str = "abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;";
str = str.replace(/abc=/gi, '');
document.write(str);

If this is what you want
1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284
then try this
var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;";
var patt1=/([\d]+)/g;
document.write(str.match(patt1));
or you can use the array index as sjngm mentioned

Related

Splitting String and taking out specific elements in node js

I have Sample String like this
"Organisation/Guest/images/guestImage.jpg"
I need to take out Organisation,Guest separately.
I have tried split() but can't get desired output.
var str = "Organisation/Guest/images/guestImage.jpg";
var res = str.split("/");
console.log(res[0]);
console.log(res[1]);
You can use of String.replace() along with regex
const regex = /Organisation\/|\/Organisation/;
console.log('Organisation/Guest/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/Organisation/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/images/guestImage.jpg/Organisation'.replace(regex, ''));
var yourString = "Organisation/Guest/images/guestImage.jpg";
yourString.split('/')
// this returns all the words in an array
yourString[0] // returns Organisation
yourString[1] // returns Guest and so on
When you run .split() on a string, it will return a new array with all the words in it. In the code I am splitting by the slash /
Then I save the new array in a variable. Now you should know we can access array properties like this: array[0] where 0 is the first index position or the first word, and so on.

javascript split string function not working

I am trying to split a string:
var str = "*HQ,6170930129,V1,185409,A,3132.3228,N,07424.7726,E,000.04,000,280618,FBFFBBFF,410,04,08028,40555#*HQ,6170930129,V1,185413,A,3132.3226,N,07424.7735,E,000.15,000,280618,FBFFBBFF,410,04,08028,40555"
var res = device_data.split('*');
But it's not working. it's just displaying this string
var str = "*HQ,6170930129,V1,185409,A,3132.3228,N,07424.7726,E,000.04,000,280618,FBFFBBFF,410,04,08028,40555#*HQ,6170930129,V1,185413,A,3132.3226,N,07424.7735,E,000.15,000,280618,FBFFBBFF,410,04,08028,40555"
var res = str.split('*');
console.dir(res)
,HQ,6170930129,V1,185409,A,3132.3228,N,07424.7726,E,000.04,000,280618,FBFFBBFF,410,04,08028,40555#,HQ,6170930129,V1,185413,A,3132.3226,N,07424.7735,E,000.15,000,280618,FBFFBBFF,410,04,08028,40555
Instead of creating an array with two elements.
IMHO, you want something like this:
var str = "*HQ,6170930129,V1,185409,A,3132.3228,N,07424.7726,E,000.04,000,280618,FBFFBBFF,410,04,08028,40555#*HQ,6170930129,V1,185413,A,3132.3226,N,07424.7735,E,000.15,000,280618,FBFFBBFF,410,04,08028,40555"
splitStrArr = str.split('*').filter(str => str != "")
console.log(splitStrArr)
console.log(splitStrArr[0])
console.log(splitStrArr[1])
You are getting a string with a period in the beginning because whatever you are doing leads to the result of String#split being converted to a string. String#split returns an array. An array converted to a string is of the form of element0,element1,element2 ... elements separated by commas.
The result of String#split in your case is ["",...] with 3 elements since your string begins with the character '*' you are searching, so String#split will create an empty string as the first element of the returned array. So the result is exactly as expected, and String#split is working as intended.
get rid of the first character of the string,
mystring.substr(1).split('*')
get rid of the empty strings
mystring.split('*').filter(s=>s!='')
to obtain the desired result.
You can use:
var res = str.split("#");
You can check in Javascript console in browser itself.
As a suggestion/ idea, you can always use the browser console, for example, Chrome browser, to execute simple scripts like these.
This way, you can save time, as it is easier to check your data structures, their internal data.
If you try
var res = str.split('*');
you obtain three elements:
res[0] is '' (empty string)
res[1] is 'HQ,61...'
res[2] is 'HQ,...'

add an element "/" in particular position in string with Javascript

I want to add an element "/" within a string, but only a specific position of it, e.g. from string "20180101" to expect result like "2018/01/01".is anyone know what syntax or how to make it happen.I'm still beginner in javascript
any help would be really appreciate.
Here is one option using replace with a regex pattern:
var input = "20180419";
console.log(input.replace( new RegExp("^(\\d{4})(\\d{2})(\\d{2})", "gm"),"$1/$2/$3"));
Use substr approach to get the result:
var str = "20180101";
str = str.substr(0, str.length-4) + '/' + str.substr(4, str.length);
str = str.substr(0, str.length-2) + '/' + str.substr(7, str.length);;
console.log(str);
Another option creating an array of sub-strings with slice (note that this would work the same way with substring here), and then joining each part with a /:
let s = "20180101"
let result = [s.substring(0,4), s.substring(4,6), s.substring(6,8)].join('/')
In such cases, you may find useful to treat your string as an array of digits.
Get an array of digits from your string:
let myString = '20180101';
let myArray = myString.split('') //the '' splitter means it will just split anything
Then, add into your array of digits, the desired '/' digit in the desired positions. Refer to the splice method for more information.
myArray.splice(4,0,'/');
myArray.splice(7,0,'/');
Then, build your string:
myString = myArray.join(''); //the opposite of splitting
console.log(myString) // outputs '2018/01/01'

Extract Twitter handlers from string using regex in JavaScript

I Would like to extract the Twitter handler names from a text string, using a regex. I believe I am almost there, except for the ">" that I am including in my output. How can I change my regex to be better, and drop the ">" from my output?
Here is an example of a text string value:
"PlaymakersZA, Absa, DiepslootMTB"
The desired output would be an array consisting of the following:
PlaymakersZA, Absa, DiepslootMTB
Here is an example of my regex:
var array = str.match(/>[a-z-_]+/ig)
Thank you!
You can use match groups in your regex to indicate the part you wish to extract.
I set up this JSFiddle to demonstrate.
Basically, you surround the part of the regex that you want to extract in parenthesis: />([a-z-_]+)/ig, save it as an object, and execute .exec() as long as there are still values. Using index 1 from the resulting array, you can find the first match group's result. Index 0 is the whole regex, and next indices would be subsequent match groups, if available.
var str = "PlaymakersZA, Absa, DiepslootMTB";
var regex = />([a-z-_]+)/ig
var array = regex.exec(str);
while (array != null) {
alert(array[1]);
array = regex.exec(str);
}
You could just strip all the HTML
var str = "PlaymakersZA, Absa, DiepslootMTB";
$handlers = str.replace(/<[^>]*>|\s/g,'').split(",");

Substring a part of a string using angularjs

I have a string like this string(1), I want to get substring remove the last part to obtain just string any suggestions please!!
PS.the string could be: sringggg(125).
You can use various options to get the desired string.
//With regular expression with split() and fetch the first element of array
console.log('sringggg(125)'.split(/\(\d+\)/)[0]);
//Using string with split() and fetch the first element of array
console.log('sringggg(125)'.split('(')[0]);
//Using substr and indexOf
var str = 'sringggg(125)'
console.log(str.substr(0, str.indexOf('(')));
References
String.prototype.split()
String.prototype.indexOf()
If string is always in same pattern and ' ( ' is a separator use split .
var string = 'string(1)'
var result = string .split('(')[0];
Try using regexp
var tesst = "string(1)"
var test = tesst.match(/^[^\(]+/);
// test = "string"

Categories

Resources