Get specific charters from raw string [duplicate] - javascript

This question already has answers here:
Javascript - return string between square brackets
(6 answers)
regex to get all text outside of brackets
(4 answers)
regex to get String which is outside of brackets
(1 answer)
Regex to extract the string both inside and outside a square bracket
(2 answers)
Closed 3 years ago.
I have a raw string like "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]". How to get value from inside of [] this box and outside of [] this box. O/P fsfsdfsdd,sdfsdfdf,sdfsd & qwewqewe,sdf If it is possible to using JavaScript RegExp?

Try this solution:
str = "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]";
s = str.match(/\[(\w+?)\]/g);
s.forEach(i => console.log(i));
Without square brackets:
res = Array.from(str.matchAll(/\[(.+?)\]/g));
res.forEach(i => console.log(i[1]));

let str = "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]";
let regex = /(?<=\[)(.*?)(?=\])/g;
let s1 = str.match(regex); // strings in the brackets
let s2 = str.replace(regex, '').replace(/[[]]/g, '').replace(/\s/, '').split(' '); // strings outside the brackets
let result = [...s1, ...s2]
console.log(result)

Related

Javascript - How to get a list of strings between two characters [duplicate]

This question already has answers here:
Regex to find or extract strings between the "<>" angle brackets
(3 answers)
Find substring between angle brackets using jQuery
(3 answers)
Regex to get string between curly braces
(16 answers)
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 months ago.
How can implement a function that returns a list of characters between two characters?
const string = "My name is <<firstname>> and I am <<age>>";
const getVariables = (string) => {
// How do I implement?
}
console.log(getVariables(string)); // ['firstname', 'age']
PS: I realize there are multiple answers on how to do something similar, but all of them only work for getting first instance and not all occurrences.
Assuming this is some kind of templating, you can proceed like this:
let format = (str, vars) =>
str.replace(/<<(\w+)>>/g, (_, w) => vars[w]);
//
const tpl = "My name is <<firstname>> and I am <<age>>";
console.log(
format(tpl, {firstname: 'Bob', age: 33})
)
You could search for string which has the starting pattern and end pattern with look behind and look ahead.
const
string = "My name is <<firstname>> and I am <<age>>",
parts = string.match(/(?<=\<\<).*?(?=\>\>)/g);
console.log(parts);
You could use regex, group, matchAll and get the first group
const string = "My name is <<firstname>>, from <<place>>, and I am <<age>>"
const getVariables = string => {
return [...string.matchAll(/<<(\w+)>>/g)].map(match => match[1])
}
console.log(getVariables(string))

RegEx for checking multiple matches [duplicate]

This question already has answers here:
How can I match overlapping strings with regex?
(6 answers)
Closed 3 years ago.
I want to match all occurrence in string.
Example:
pc+pc2/pc2+pc2*rr+pd
I want to check how many matched of pc2 value and regular expression is before and after special character exists.
var str = "pc+pc2/pc2+pc2*rr+pd";
var res = str.match(new RegExp("([\\W])pc2([\\W])",'g'));
But I got only +pc2/ and +pc2* and /pc2+ not get in this.
Problem is in first match / is removed. So after that, it is starting to check from pc2+pc2*rr+pd. That's why /pc2+ value does not get in the match.
How do I solve this problem?
You need some sort of recursive regex to achieve what you're trying to get, you can use exec to manipulate lastIndex in case of value in string is p
let regex1 = /\Wpc2\W/g;
let str1 = 'pc+pc2/pc2+pc2*rr+pd';
let array1;
let op = []
while ((array1 = regex1.exec(str1)) !== null) {
op.push(array1[0])
if(str1[regex1.lastIndex] === 'p'){
regex1.lastIndex--;
}
}
console.log(op)

Regex capturing series of alphanumeric and numeric characters into an array [duplicate]

This question already has answers here:
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 4 years ago.
I am not good at regex patterns, I can get everything inside the curly parenthesis with {(.*?)} but I cannot split them.
Suppose I have a string like this
{y12.13bb15.16}
How do I capture it like this into an array:
['y', '12.13', 'bb', '15.16']
although in the end essentially I wanna create an object like this:
{"y": 12.13, "bb": 15.16}
You may use ([\d\.]+|[^\d{}]+)
var rx = /([\d\.]+|[^\d{}]+)/g
s = '{y12.13bb15.16}'
k = s.match(rx)
console.log(k)
// And now to convert to your desired object
var final = {}
for (i = 0; i < k.length; i += 2) {
final[k[i]] = k[i+1]
}
console.log(final)

Regex replace with captured [duplicate]

This question already has answers here:
Using $0 to refer to entire match in Javascript's String.replace
(2 answers)
Closed 5 years ago.
I want replace all non alphanumeric character in the string by it self surrender for "[" and "]".
I tried this:
var text = "ab!#1b*. ef";
var regex = /\W/g;
var result = text.replace(regex, "[$0]");
console.log(result);
I was expecting to get:
ab[!][#]1b[*][.][ ]ef
But instead i get:
ab[$0][$0]1b[$0][$0][$0]ef
How can I do this using Javascript(node)?
You need to wrap the group in parentheses to assign it to $1, like this:
var text = "ab!#1b*. ef";
var regex = /(\W)/g;
var result = text.replace(regex, "[$1]");
console.log(result);

How can I extract only the integer from a String JavaScript [duplicate]

This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
Closed 6 years ago.
I have a string that looks like:
var a = "value is 10 ";
How would I extract just the integer 10 and put it in another variable?
You could use a regex:
var val = +("value is 10".replace(/\D/g, ""));
\D matches everything that's not a digit.
you can use regexp
var a = "value is 10 ";
var num = a.match(/\d+/)[0] // "10"
console.log ( num ) ;
You can use some string matching to get an array of all found digits, then join them together to make the number as a string and just parse that string.
parseInt(a.match(/\d/g).join(''))
However, if you have a string like 'Your 2 value is 10' it will return 210.
You do it using regex like that
const pattern = /\d+/g;
const result = yourString.match(pattern);

Categories

Resources