Split Array Object Value in javascript [duplicate] - javascript

This question already has answers here:
Javascript How to get first three characters of a string
(2 answers)
Keep only first n characters in a string?
(7 answers)
Closed 7 days ago.
I'm confused where I want to split the correspondence_code value like in the image below : let say I want to get just the first 5 digits like 16020 it is possible just getting the first 5 value? Thanks in advance
var result = objt['psgc'].filter(obj => {
return obj.geographic_level === "province"
})
// I want to split the value result -> correspondence_code with only first 5 digits

Related

Split multiple text (Dynamically) in js [duplicate]

This question already has answers here:
How do you split a javascript string by spaces and punctuation?
(4 answers)
How to parse string into words and punctuation marks using javascript
(2 answers)
Closed 9 months ago.
Here I have text something like this:
const string = 'Welcome;;to::';
const string ='Welcome;;';
I want to split this string with ;; and ::;
Expected result:
['Welcome', ';;', 'to', '::']
There will be chance that number of pattern will be increased.
So, is this possible ?
Note: There is no space between words.

I want the following regex to return 16 characters [duplicate]

This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I have the following code and want to be able to only return 10 characters after the 'LAST='. If there isn't 10 chars then don't return anything.
const value = 'LAST=uirndheusnrm38f6xxxxxxx'
const m = value.match(/LAST=([^\?&]+)/i)
if (m)
{
console.log('i know it matched LAST= and returned the next 10 chars')
}
Is it possible without doing a substr(0,10) ?
In order to match exactly 10 characters we can use {10}:
const m = value.match(/LAST=([^\?&]{10})/i);

JS Regex to extract all URL from a Text to array of URL [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expression to match string starting with a specific word
(10 answers)
Regular expression - starting and ending with a character string
(3 answers)
Closed 3 years ago.
I am trying to pull out all the URLs from a text entered by the user by doing the following but am not able to get the desired result.
let regexp = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$/igm;
let str = "https://mysleepyhead.com http://skreem.io";
let array = [...str.matchAll(regexp)];
console.log(array);
Desired output would be
Array ['https://mysleepyhead.com', 'http://skreem.io']

How to get a substring before the nth point in javascript [duplicate]

This question already has answers here:
Javascript nth occurance of a string and extract the sub string upto that
(3 answers)
Regex expression to cut string at nth occurrence of character and return first part of string
(3 answers)
Cutting a string at nth occurrence of a character
(5 answers)
Closed 3 years ago.
I want to get a substring that is before the nth point.
For example I have:
let str = "my.string.is.like.that"
Now suppose I want substr= "my.string.is.like" that is all before the 4th point. How can I do that?
You can use split and join,
Second parameter in split is used to limit the number of element to be included in final output
let str = "my.string.is.like.that"
let limited = str.split('.',4).join('.')
console.log(limited)

getting a number using regular expressions [duplicate]

This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333

Categories