Call Substring multiple times - javascript

I have a string and I want to remove this data like this \(f(x) = (a+b)\)
so i am thinking to get all subsstring and then make some operation on array. But it is giving me only one stripedHtml. So not able to get how to clean it. Just wants to remove this equations.
Result will be : Here’s the evidence
const filter_data = `<p>\(f(x) = (a+b)\)</p><p>\(f(x) = (a+db)\)</p><p>\(f(x) = (a+d+c+b)\)</p>
<p>Here’s the evidence.</p>`
var strippedHtml = filter_data.substring(
filter_data.lastIndexOf("\(") + 1,
filter_data.lastIndexOf("\)")
);
console.log(strippedHtml)

JS has a replace method for this that accepts RegExp:
const filter_data = `<p>\(f(x) = (a+b)\)</p><p>\(f(x) = (a+db)\)</p><p>\(f(x) = (a+d+c+b)\)</p>
<p>Here’s the evidence.</p>`;
var strippedHtml = filter_data.replace(/\<.*?\(.*?>/g, "");
console.log(strippedHtml);
The RegExp searches for an < followed by a ( and then an > and replaces all appearances with an empty value.
In your string it will match two times and do a replace.
Maybe you have to modify the RegExp to fit your real string as it would also match text nodes containing ( but that's what I would do at this point with the given data.

You can use following regular expressions to obtain solution for only similar type of data you were provided
const filterData1 = `<p>\(f(x) = (a+b)\)</p><p>\(f(x) = (a+db)\)</p><p>\(f(x) = (a+d+c+b)\)</p><p>Here’s the evidence.</p>`
const filterData2 = `<p>\(f(x) = (a+b)\)</p><p>\(f(x) = (a+db)\)</p><p>\(f(x) = (a+d+c+b)\)</p><p>Here’s the evidence.</p><p>\(f(x) = (a+b)\)</p>`
const regEx1 = /<[^>]*>/g //regular expression to remove all html tags
const regEx2 = /\([^\)]*\)/g //regular expression to remove everything between \( and \)
const regEx3 = /[=)]/g //regular expression to remove = and )
const result1 = filterData1.replace(regEx1,'').replace(regEx2,'').replace(regEx3,'').trim()
const result2 = filterData2.replace(regEx1,'').replace(regEx2,'').replace(regEx3,'').trim()
console.log("Result1 : ",result1);
console.log("Result2 : ",result2);

Related

How to find text between 2 characters with multiple occurrences?

Given the below string, what would be the most efficient way to get the file ID? The portion wanted: XXXXXXXXXXXxxxxxxxxXXX, which is between / and /view
The attempt below works, but is it really needed to reverse the string twice?
let = url = 'https://drive.google.com/file/d/1pnEX1OXXXXXXu6z9dPV5ZZ5VHqPU--6/view?usp=share_link'
url = reverseString(url)
let id = url.split('weiv/').pop().split('/')[0]
id = reverseString(id)
console.log('URL:' + id)
function reverseString(str) {
var splitString = str.split("");
var reverseArray = splitString.reverse();
var joinArray = reverseArray.join("");
return joinArray;
}
This solution searches for the "/d/" portion and advances three characters to begin a string.slice, continuing until the next occurence of /. Provided /d/ is always before the id portion, this should be reliable.
const url = 'https://drive.google.com/file/d/1pnEX1OXXXXXXu6z9dPV5ZZ5VHqPU--6/view?usp=share_link';
const id = url.slice(url.indexOf("/d/")+3, url.indexOf("/",url.indexOf("/d/")+3 ));
console.log(id);
I'd solve this with a simple regex.
const url = 'https://drive.google.com/file/d/1pnEX1OXXXXXXu6z9dPV5ZZ5VHqPU--6/view?usp=share_link';
const m = url.match(/^.*?\/.\/(.*?)\/view.*$/);
console.log(m[1])
you can use substring to get the value between /d/ and /view
let = url = 'https://drive.google.com/file/d/1pnEX1OXXXXXXu6z9dPV5ZZ5VHqPU--6/view?usp=share_link'
const fileId = url.substring(url.lastIndexOf("/d/") + 3, url.lastIndexOf("/view"));
console.log(fileId)

Matching function name and arguments using regex

I have some strings in the following pattern
'walkPath(left, down, left)'
To extract the function name alone, and the arguments in an another array, i used those regular expressions:
const str = 'walkPath(left, down, left)'
const functionNameRegex = /[a-zA-Z]*(?=\()/
console.log(str.match(functionNameRegex)) //outputs ['walkPath'] ✅✅
const argsRegex = /(?![a-zA-Z])([^,)]+)/g
console.log(str.match(argsRegex)) //outputs [ '(left', ' down', ' left' ]
the first one worked fine. In the second regex, the '(' from from '(left' should be excluded, so it should be 'left'
Try this one:
/(?<=\((?:\s*\w+\s*,)*\s*)\w+/g
const str = 'walkPath(left, down, left)'
const functionNameRegex = /[a-zA-Z]*(?=\()/
console.log(str.match(functionNameRegex))
const argsRegex = /(?<=\((?:\s*\w+\s*,)*\s*)\w+/g
console.log(str.match(argsRegex))
It is not very restricted, if you really want to be safe, you can try:
/(?<=\w+\s*\((?:\s*\w+\s*,\s*)*\s*)\w+(?=\s*(?:\s*,\s*\w+\s*)*\))/g
Use this regular expression for getting the arguments:
const argsRegex = /\(\s*([^)]+?)\s*\)/
For getting the arguments in an array:
const str = 'walkPath(left, down, left)'
const argsRegex = /\(\s*([^)]+?)\s*\)/
let res = str.match(argsRegex)
let args = res[1].split(", ")

Javascript get only matched text in regex

I have string like below
BANKNIFTY-13-FEB-2020-31200-ce
I want to convert the string to 13-FEB-31200-ce
so I tried below code
str.match(/(.*)-(?:.*)-(?:.*)-(.*)-(?:.*)-(?:.*)/g)
But its returning whole string
Two capture groups is probably the way to go. Now you have two options to use it. One is match which requires you to put the two pieces together
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.match(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/)
// just reference the two groups
console.log(`${match[1]}${match[2]}`)
// or you can remove the match and join the remaining
match.shift()
console.log(match.join(''))
Or just string replace which you do the concatenation of the two capture groups in one line.
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.replace(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/, '$1$2')
console.log(match)
Regex doesn't seem to be the most appropriate tool here. Why not use simple .split?
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.split('-');
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
If you really want to use regexp,
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.match(/[^-]+/g);
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
I would not use Regex at all if you know exact positions. Using regex is expensive and should be done differently if there is way. (https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)
const strArr = "BANKNIFTY-13-FEB-2020-31200-ce".split("-"); // creates array
strArr.splice(0,1); // remove first item
strArr.splice(2,1); // remove 2020
const finalStr = strArr.join("-");
If the pattern doesn't need to be too specific.
Then just keep it simple and only capture what's needed.
Then glue the captured groups together.
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let m = str.match(/^\w+-(\d{1,2}-[A-Z]{3})-\d+-(.*)$/)
let result = m ? m[1]+'-'+m[2] : undefined;
console.log(result);
In this regex, ^ is the start of the string and $ the end of the string.
You can have something like this by capturing groups with regex:
const regex = /(\d{2}\-\w{3})(\-\d{4})(\-\d{5}\-\w{2})/
const text = "BANKNIFTY-13-FEB-2020-31200-ce"
const [, a, b, c] = text.match(regex);
console.log(`${a}${c}`)

javascript regex - match between slashes

I have a URL: https://api.example.com/main/user1/collection1/contents/folder1/test.js
To capture only collection1, I tried the following but it returns user1 along with it, how can I exclude user1?
(?:\/user1\/)[^\/]*
To capture only folder1/test.js, the following also returns contents and I want to exclude it
contents\/(.*)$
If you want to use a regex. you could try (?:\/user1\/)([^\/]+) to capture only collection1.
This captures the part after /user1/ in a capturing group ([^\/]+).
const pattern = /\/user1\/([^\/]+)/;
const str = "https://api.example.com/main/user1/collection1/contents/folder1/test.js";
matches = str.match(pattern);
console.log(matches[1]);
To capture only folder1/test.js you could use folder1\/.*$
const pattern = /folder1\/.*$/;
const str = "https://api.example.com/main/user1/collection1/contents/folder1/test.js";
matches = str.match(pattern);
console.log(matches[0]);
Without regex you might use URL:
const str = "https://api.example.com/main/user1/collection1/contents/folder1/test.js";
const url = new URL(str);
const parts = url.pathname.split('/');
console.log(parts[3]);
console.log(parts[5] + '/' + parts[6]);

replace a string partially with something else

lets say I have this image address like
https://firebasestorage.googleapis.com/v0/b/myproj-d.appspot.com/o/FILE_NAME.jpg?alt=media&token=124bb2bf-c6ef-432b-92c7-7032563ba31b
how is it possible to replace FILE_NAME.jpg with THUMB_FILE_NAME.jpg
Note: FILE_NAME and THUMB_FILE_NAME are not static and fix.
the FILE_NAME is not fixed and I can't use string.replace method.
eventually I don't know the File_Name
Use replace
.replace(/(?<=\/)[^\/]*(?=(.jpg))/g, "THUMB_FILE_NAME")
or if you want to support multiple formats
.replace(/(?<=\/)[^\/]*(?=(.(jpg|png|jpeg)))/g, "THUMB_FILE_NAME")
Demo
var output = "https://firebasestorage.googleapis.com/v0/b/myproj-d.appspot.com/o/FILE_NAME.jpg?alt=media&token=124bb2bf-c6ef-432b-92c7-7032563ba31b".replace(/(?<=\/)[^\/]*(?=(.jpg))/g, "THUMB_FILE_NAME");
console.log( output );
Explanation
(?<=\/) matches / but doesn't remember the match
[^\/]* matches till you find next /
(?=(.jpg) ensures that match ends with .jpg
To match the FILE_NAME, use
.match(/(?<=\/)[^\/]*(?=(.(jpg|png|jpeg)))/g)
var pattern = /[\w-]+\.(jpg|png|txt)/
var c = 'https://firebasestorage.googleapis.com/v0/b/myproj-d.appspot.com/o/FILE_NAME.jpg?alt=media&token=124bb2bf-c6ef-432b-92c7-7032563ba31b
'
c.replace(pattern, 'YOUR_FILE_NAME.jpg')
you can add any format in the pipe operator
You can use the String's replace method.
var a = "https://firebasestorage.googleapis.com/v0/b/myproj-d.appspot.com/o/FILE_NAME.jpg?alt=media&token=124bb2bf-c6ef-432b-92c7-7032563ba31b";
a = a.replace('FILE_NAME', 'THUMB_FILE_NAME');
If you know the format, you can use the split and join to replace the FILE_NAME.
let str = "https://firebasestorage.googleapis.com/v0/b/myproj-d.appspot.com/o/FILE_NAME.jpg?alt=media&token=124bb2bf-c6ef-432b-92c7-7032563ba31b";
let str_pieces = str.split('/');
let str_last = str_pieces[str_pieces.length - 1];
let str_last_pieces = str_last.split('?');
str_last_pieces[0] = 'THUMB_' + str_last_pieces[0];
str_last = str_last_pieces.join('?');
str_pieces[str_pieces.length - 1] = str_last;
str = str_pieces.join('/');

Categories

Resources