How to extract a string from parent string? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string url as follows:
var url = '/test/mybin/processes/edit.jsp?id={processId}';
I want to extract the string "processId" from above url. I can get it using indexOf and substring methods, but is there a better alternative to do it? Can we do it using Regex?

var procid = url.split("id=")[1];

You can easily use a regex:
url.match(/\{([^}]*)\}/)[1];
But for this simple pattern, using indexOf and substring, while not as terse, will have much better performance. TIMTOWTDI

'/test/mybin/processes/edit.jsp?id={processId}'.split('{')[1].split('}')[0]

JavaScript strings has regex support embeded:
var processId = url.match(/\?id=(.+)/)[1];
Only thing U need - be familiar with regular expressions
And if braces are problem:
var processId = url.match(/\?id=\{(.+)\}/)[1];

Related

Javascript remove all ',' in a string with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i want remove all ',' from my string with regex in javascript.
this is an example from my string:
45,454,545
and i want my string convert to this:
45454545
Comma isn't a special character in regex, so you can just use /,/. Add the global flag and you're done.
console.log('45,454,545'.replace(/,/g, ''))
Try this,
var str = "45,454,545";
var res = str.replace(/,/g, "");
console.log(res);

How to remove a group of characters from a phone string in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am willing to know how can I check if the a phone number contain the following prefixes +44, 0044 or 0 and if so it will be removed? I know I to remove a number of characters or a substring but how do I check if that substring is in the beginning?
Thanks in advance
You could with regex /^(\+44|0044|0)/g
function rem(str){
return str.replace(/^(\+44|0044|0)/g,'')
}
console.log(rem('0044987987'));
console.log(rem('04478687'));
console.log(rem('+447783'));
You can use indexOf to check the index of those prefix.
"+44-----".indexOf("+44") // returns 0
That should be enough for what you want.
You can simply test against a regex pattern.
Or use it for a replace.
const re_tel44 = /^[+]?0{0,2}(?:44)?(\d{5,})$/;
let phonenumber = '+4412345';
console.log(re_tel44.test(phonenumber));
if(re_tel44.test(phonenumber))
phonenumber = phonenumber.replace(re_tel44, '$1');
console.log(phonenumber);

How to retrieve substring which is outside of brackets in JS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want the text which is outside of brackets, for eg.
Text is - Outside (inside)
and what I expect is - Outside
Can someone please help me to achieve this.
You can use slice & use indexOf to get the first (. This will extract all the characters before first (
let str = 'Outside (inside)'
let substr = str.slice(0, str.indexOf('('));
console.log(substr.trim())
If you wanted to remove all bracketed text from the string you could use
let str = 'Outside (inside)test(d 342 dd3d)dd(t423t t)dd()fasf(fsdfds32dfs)';
console.log(str.replace(/(\([\w\d ]*\))+/g, ''))

Regex in JavaScript replace function break script after uglifyjs or minify run [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Could someone with more regex experience than me help me out?
return path.replace(/\//g, '.').replace(/^\./, '');
I have found this regex in a js file within a giant app. The JS when run through npm node-minify or any of the others sees it as a comment and turns it into this:
return path.replace(/\g, '.').replace(/^\./, '');
I get the first bit is replacing all \ with a . and the second bit trims any leading . from the string. Can i change this so the regex pattern is wrapped in quotes?
Just use the RegExp constructor and quote your pattern.
const path = '/usr/bin/env';
const matchSlash = new RegExp('/', 'g');
const translate = path => path.replace(matchSlash, '.').replace(/^\./, '');
console.log(translate(path));

Split string using javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to split${SOMENAME} (${THISNAME}) ${THESENAME}
I just need to extract the words SOMENAME THISNAME and THESENAME from the above string. Is it possible?
You can pass in a regular expression separator as part of the .split() function.
var string = "${SOMENAME} (${THISNAME}) ${THESENAME}";
var re = /\W+/;
var arr = string.split(re);
document.write(arr);
Take a look at String.prototype.split for more information.
If you only need extract the words, this could be a simple solution:
var s = "${SOMENAME} (${THISNAME}) ${THESENAME}";
var words = s.match(/([A-Z])\w+/g);

Categories