Split the string between the two characters? [duplicate] - javascript

This question already has answers here:
RegEx to match stuff between parentheses
(4 answers)
Closed 6 months ago.
I had the following string ,
He is #(role)
I need to get the string which is present between #( and ).
Expected result ,
role

We can use match() here:
var input = "He is #(role)";
var role = input.match(/#\((.*?)\)/)[1];
console.log(role);

You have to use the split method of string which split the string where you want, answer of the question here:
const string = 'He is #(role)';
const answer = string.split('#');
if you console.log(answer) then the console show
['He is ', '(role)'];

const arrMatch = 'He is #(role)'.match(/\#\(([^\)]+)/)
const text = arrMatch[1]
console.log(text)

here is the best and simple way to do this with string slice method which helps you to splice the exact string which you want.
Here is the answer
const string = 'He is #(role)';
const answer = string.slice(8, 12);
console.log(answer);
Now you can see the result 'role'
Explain: here is the resource which helps you learn this method
https://www.w3schools.com/jsref/jsref_slice_string.asp#:~:text=The%20slice()%20method%20extracts,of%20the%20string%20to%20extract.

Related

How to get the part of the string after the 2nd space [duplicate]

This question already has answers here:
Cutting a string at nth occurrence of a character
(5 answers)
Closed 1 year ago.
I am having a string like: $m-set 88829828277 very good he is. From this string I want to get the part after the second space. ie, I want to get: very good he is.
I tried using split(" ")[2] but it only gives one word: very.
Any help is greatly appreciated!
Thanks!
While you could split and join:
const input = '$m-set 88829828277 very good he is';
const splits = input.split(' ');
const output = splits.slice(2).join(' ');
console.log(output);
You could also use a regular expression:
const input = '$m-set 88829828277 very good he is';
const output = input.match(/\S+ \S+ (.+)/)[1];
console.log(output);
where the (.+) puts everything after the second space in a capture group, and the [1] accesses the capture group from the match.

Replace values from an array in string non case sensitive in javascript [duplicate]

This question already has answers here:
Case insensitive replace all
(7 answers)
Closed 2 years ago.
My code is as follows:
array.forEach(el => {
string = string.replace(el, `censored`);
});
array : my array of words that I want to censor.
string : the string that the words need censoring.
My issue is that this process is quite slow and also if the word in my string is written using capitals, it's getting missed.
Any ideas how should I solve this issue?
Thank you.
maybe you can use regex
let array = ['mate']
let string = 'Hello Mate, how are you mate?'
let re = new RegExp(array.join("|"),"gi");
let str = string.replace(re, 'censored');
output:
"Hello censored, how are you censored?"

JavaScript Regex Question. How to split this string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm new to Regex and was wondering how to split this string s4://test-dev/prefixes/file.mxf into ['test-dev', 'prefixes/file.mxf']. I want it to work for an unknown file path.
Ex)
s4://test-dev/prefixes/file.mxf/newpath/anothernewpath into
['test-dev', 'prefixes/file.mxf/newpath/anothernewpath']
If you are using regex for splitting (which is not necessary) you can use this regex: s4:\/\/test-dev\/(.*) and use first group (first parentheses) as second string (first one is always same as i can see), but easiest way is to find position of third '/' with this var pos=str.split("/", i).join("/").length; and then find substring from that position to end: var res = str.substring(pos, str.length-pos);
Instead of splitting, use capture groups.
url.match(/^[^/]+:\/\/([^/]+)\/?(.*)/).slice(1)
Please check the code below:
let url = "s4://test-dev/prefixes/file.mxf/newpath/anothernewpath"
let match = url.match(/^s4:\/\/([\w-]+)\/(.+)$/)
let result = [match[1], match[2]]
console.log(result)
The result is:
[
"test-dev",
"prefixes/file.mxf/newpath/anothernewpath"
]
/^[\w+\:\/\/]+[-w+]/i
matches 'text-dev'
/w+\:\/\/[-w+]\/gi/
matches the rest

regex to replace anything that is not a number or period in string [duplicate]

This question already has answers here:
Regex to replace everything except numbers and a decimal point
(7 answers)
Closed 3 years ago.
I am trying to write a regex that replaces anything that isn't a digit or a . in a string.
For example:
const string = 'I am a 1a.23.s12h31 dog'`
const result = string.replace(/[09.-]/g, '');
// result should be `1.23.1231`
Can anyone see what I am doing wrong here.
You could change your regex to [^0-9.]+:
const result = string.replace(/[^0-9.]+/g, "");
Alternatively, if you don't want a regex, use split and filter, then join:
const result = string.split("").filter(s => isNaN(s) || s == ".").join("");

Looking to trim a string using javascript / regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I'm looking for some assistance with JavaScript/Regex when trying to format a string of text.
I have the following IDs:
00A1234/A12
0A1234/A12
A1234/A12
000A1234/A12
I'm looking for a way that I can trim all of these down to 1234/A12. In essence, it should find the first letter from the left, and remove it and any preceding numbers so the final format should be 0000/A00 or 0000/AA00.
Is there an efficient way this can be acheived by Javascript? I'm looking at Regex at the moment.
Instead of focussing on what you want to strip, look at what you want to get:
/\d{4}\/[A-Z]{1,2}\d{2}/
var str = 'fdfhfjkqhfjAZEA0123/A45GHJqffhdlh';
match = str.match(/\d{4}\/[A-Z]{1,2}\d{2}/);
if (match) console.log(match[0]);
You could seach for leading digits and a following letter.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'],
regex = /^\d*[a-z]/gi;
data.forEach(s => console.log(s.replace(regex, '')));
Or you could use String#slice for the last 8 characters.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'];
data.forEach(s => console.log(s.slice(-8)));
You could use this function. Using regex find the first letter, then make a substring starting after that index.
function getCode(s){
var firstChar = s.match('[a-zA-Z]');
return s.substr(s.indexOf(firstChar)+1)
}
getCode("00A1234/A12");
getCode("0A1234/A12");
getCode("A1234/A12");
getCode("000A1234/A12");
A regex such as this will capture all of your examples, with a numbered capture group for the bit you're interested in
[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})
var input = ["00A1234/A12","0A1234/A12","A1234/A12","000A1234/A12"];
var re = new RegExp("[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})");
input.forEach(function(x){
console.log(re.exec(x)[1])
});

Categories