I need your help again with a regex.
I have this string:
some string
text1
name="yyy", path="C:/asdf/qwer.jpg" type="jjj"
text2
name="3yu", path="C:/asdf/12t2.mov" type="uuu"
And I have this regex by far:
/\b(string\b(.|\n)*?)\btext1\b/
Now, I need to replace the path of text1 so the output will be:
some string
text1
name="yyy", path="D:/here/is/my/another/path.png" type="jjj"
text2
name="3yu", path="C:/asdf/12t2.mov" type="uuu"
I know that I need to use this also (lmk if I wrong, also what is better to use?):
/".*?"/
or:
/"[^"]*"/
But how do I tell it to replace like I need?
You can replace that string this way:
var re = /(string[\s\S]*?text1[\s\S]*?\bpath=")[^"]+/;
var str = 'some string\n\ntext1\n\nname="yyy", path="C:/asdf/qwer.jpg" type="jjj"\n\ntext2\n\nname="3yu", path="C:/asdf/12t2.mov" type="uuu"';
var subst = '$1D:/here/is/my/another/path.png';
document.write(str.replace(re, subst).replace(/\n/g,"<br/>"));
The regex is
/(string[\s\S]*?text1[\s\S]*?\bpath=")[^"]+/
EXPLANATION:
(string[\s\S]*?text1[\s\S]*?\bpath=") - a capturing group that will capture the text into group 1 that we'll refer to later usign a $1 backreference in the replacement string. It matches:
string - literal string
[\s\S]*? - any 0 or more characters, as few as possible
text1 - text1 literally
[\s\S]*? - any 0 or more characters, as few as possible
\bpath=" - matches path=" literally with a non-word character (e.g. space) before path (you may remove it if it is part of a longer word)
[^"]+ - 1 or more characters other than ".
(\bstring\b[\s\S]*?\btext1\b(?:(?!\btext\d+\b)[\s\S])*path=")[^"]*
You can use this.Replace by $1mypath.See demo.
https://regex101.com/r/fX3oF6/3
var re = /(\bstring\b[\s\S]*?\btext1\b(?:(?!\btext\d+\b)[\s\S])*path=")[^"]*/g;
var str = 'some string\n\n text1\n\n name="yyy", path="C:/asdf/qwer.jpg" type="jjj"\n\n text2\n\n name="3yu", path="C:/asdf/12t2.mov" type="uuu"\n';
var subst = ' $1mypath';
var result = str.replace(re, subst);
Related
I need help in javascript where the word entered can be replaced as:
Input - A.. BC
Output - A-BC
the code that i have tried is:
var text = 'A.. BC';
new_text = text.replace(' ', '-') && text.replace('.','-');
console.log(new_text);
This is not working as it is giving me the output as:
A-. BC
I'd use a regular expression instead. Use a character set to match one or more dots, commas, or spaces, then replace with a dash:
const change = str => str.replace(/[., ]+/g, '-');
console.log(change('A.. BC'));
Use a charater set
var text = 'A.. BC';
new_text = text.replace(/[., ]+/g, '-');
console.log(new_text);
you can try replacing all non-alphabetical characters with a hyphen with regex:
const a = 'A.. BC';
const b = 'A ..BC';
// Find all non-word characters regex
const r = /[\W]+/g;
const hyphen = '-';
void console.log(a.replace(r, hyphen));
void console.log(b.replace(r, hyphen));
// A-BC
// A-BC
I trying to remove some string from string which I have.
I have following string.
"[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
I want to remove text between ^ # including that character.
Output should be "[1fe3-46675-be1a-cd97084b] dsd dsds [4j34-46675-be1a-cd97854b]"
I used following but, not removing that string.
let str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
str = str.replace(/^.*#/g, '');
console.log(str);
You can do it with this regex.
let stringsS = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
let regex = /\^(.*?)\#/gi
console.log(stringsS.replace(regex,''));
Try this:
str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#";
replacedStr = str.replace(/\^[^#]*\#/g, '');
console.log(replacedStr)
Escape the ^ as in:
str = str.replace(/\^.*#/, '');
The ^ means something in regex and must be escaped if you want it to be treated as normal character
How to Replace -84 in a string: my-name-is-dude-84 with '' Regex?
I means the last '-' + number
I tried :
string = 'my-name-is-dude-84';
let regex = /[^\-*][1-9]/;
let specialChar = string.replace(regex, '');
then I received is my-name-is-dude-
I expect my string will be: my-name-is-dude
You're close, but this is what you need to do (I guess)
string = 'my-name-is-dude-84';
let regex = /-\d+$/;
let specialChar = string.replace(regex, '');
document.write(specialChar);
Your [^\-*] tries to match all characters but \, - and *. Also [1-9] only matches one digit (between 1 and 9). Use \d (all digits), and add a + to make it match one or more. Also, adding an end of string anchor $ to it makes it only match the hyphen+number at the end of the string.
You can use this regex (.*?)-\d+$
regex demo
JavaScript demo
string = 'my-name-is-99-dude-84';
let regex = /(.*?)-\d+$/;
let specialChar = string.replace(regex, "$1");
document.write(specialChar);
The best way to explain this is by example. I'm using jQuery to do this.
Example I have a string
var str = "1.) Ben"
how can I dynamically omit the character 1.) including the space such that str === "Ben"
str can be dynamic such that order can increment from ones, tens, to hundreds.
E.G.
var str = "52.) Ken Bush"
or
var str = "182.) Hailey Quen"
Expected output
str === "Ken Bush"
or
str === "Hailey Quen"
Example
var str = "182.) Hailey Quen"
var test = str.split(') ');
test = test[1];
//output "Hailey Quen"
You can use regex replacement to get what you want.
var str = "182.) Hailey"
var newStr = str.replace(/^\d+\.\)\s*/, '')
// Hailey
var s = "1456.) Hard Spocker".replace(/^\d+\.\)\s*/, '')
// Hard Spocker
^ makes sure that the pattern is matched at the start of the string only
\d+ will match one or more digits.
\. will match the . with escaping
) is a symbol so we need to escape it using \ as \)
\s* will match one or more spaces.
You can learn about these symbols here.
Try using .substring() and .indexOf() as shown :-
var str = "182.) Hailey Quen"
alert(str.substring(str.indexOf(' ')))
DEMO
OR use .split() as shown :-
var str = "182.) Hailey Quen"
alert($.trim(str.split(')')[1]))
DEMO
You can do it regular expression,
var str = "52.) Ken".replace(/\d+\.\)\s/g,"");
console.log(str); //Ken
DEMO
If you have zero or more than zero spaces after the ) symbol then you can use *,
var str = "52.) Ken".replace(/\d+\.\)\s*/g,"");
console.log(str); //Ken
Dismantling regex used,
/ states regex left border
\d d states normal character d, if we want to make it match
numbers then we have to escape it with \
+ It states that one or more number should be there.
\. Again . is a metacharacter to match any valid character, so
escape it.
\) Parenthesis is also a metacharacter to close a group, escape
it.
\s* 12.) can be followed by zero or more spaces.
/ states regex right boundary.
g global flag, which used to do a search recursively.
You can do it like this
var testURL = "182.) Hailey Quen";
var output = testURL.substring(testURL.lastIndexOf(")") + 1).trim();
console.log(output);
*trim function will help to remove extra space if any.Hope it will help
Please help.
I need a regular expression (to be used in javascript) to replace "." with "#" in a text containing Unicode characters.
Replacement takes place only when "." appears between text but not between digits.
Input: "ΦΨ. ABC. DEF. 123.456"
Desired output: "ΦΨ# ABC# DEF# 123.456"
Any suggestions?
You can use capturing groups in the regex and use back-references to obtain the required result:
var re = /(\D)\.(\D)/g;
var str = 'ΦΨ. ABC. DEF. 123.456';
var subst = '$1#$2';
result = str.replace(re, subst);
alert(result);
Regex Explanation:
\D - A non-digit character
\. - A literal dot
The non-digit characters are captured into groups, and then inserted back with the help of $1 and $2 back-references.
try this:
var str = "ΦΨ. ABC. DEF. 123.456";
str.replace(/[^\d.]+\.[^\d]/g, function (m) {
return m.replace('.', '#')
});