Replace all String with empty string starting with -- [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i have string which is
testVariable--423h33c7uhyga5tjk
now i want to replace the above string with
testVariable
by using javascript replace function.

Use String#split method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace('--')[0]
)
Or with String#replace method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace(/--.*/, '')
// or including multiline
// .replace(/--[\s\S]*/, '')
)

While Pranav's method work, if you really need/Want to use the replace function, you could use regex:
var variable = 'testVariable--423h33c7uhyga5tjk';
console.log(variable.replace(/--.+$/, ''));

Related

How can I insert a backslash (\) character using replace()? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to use the replace function in a forEach loop but it's not working:
var x = [".em", ".one"];
x.forEach((val, index) => {
console.log(val.replace(".", "\."));
});
The issue is because the \ character is the escape character in JS. If you want to output an actual \ in the string, you need to use two of them:
var x = [".em", ".one"];
x.forEach((val, index) => {
console.log(val.replace(".", "\\."));
});

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, ''))

Javascript regex: issue when trying to parse both http and https instances of a natively archived string URL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
for example, consider,
what regex code is one supposed to use to get the last URL in the string , that is "iskme" in the above URL.
Example 1:
string 1-> "https://web.archive.org/web/204534534534645/http://www.iskme.org:80/"
result-> iskme . org
string 2 -> "https://web.archive.org/web/24534534642321/https://www.nytimes.com"
result: nytimes .com
what will be the common regex code for the above two examples:
I am currently using http:?//\w\w\w?\S+.\S\S\S
This regex is satisfying example 1 but fails in Example 2, where it fails to parse the 2nd string during the instance/occurrence of "NYTimes" main content URL.
I am new to Regex and tried to find the answer within google and understood that I needed to add the HTTP(s)? condition. But, it still seems to fail, can anyone point me in the right direction so I can solve this problem, Thank you in advance.
A simpler expression should do the trick:
var str = "https://web.archive.org/web/20030328195612/https://www.iskme.org:80/";
var url = str.match(/.*(https?:.*)/)[1];
The first .* will consume as many characters as possible up until the last occurrence of http(s): in the search string.
Answer above by #buttonupbub does the trick.
But if you need, for any reason, to store both urls for use after any string parse:
'https://web.archive.org/web/20030328195612/https://www.iskme.org:80/'
.split('http')
.reduce((acc, curr) => {
if (curr) {
acc.push( 'http' + curr )
}
return acc;
}, []);
// returns ["https://web.archive.org/web/20030328195612/", "https://www.iskme.org:80/"]
Making a lot of assumptions about the data here.

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));

How to extract a string from parent string? [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 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];

Categories