Regex need to includes the plus sign - javascript

I have the following problem with my regex.
I want to search a string between two strings.
The datas is like that:
var datas = "a='00-8'b='13-'a+='00-2'b+='3333'c='4'";
I try:
datas.match("a\+='(.*?)'");
I can't get the regex working due to the + sign.
Any help ?

You're passing a String into match, not a RegExp, perhaps you wanted
datas.match(/a\+='(.*?)'/);
Alternatively, you need to escape your backslash for the String so it can escape the + as a RegExp, i.e.
datas.match("a\\+='(.*?)'");

Enclose the regex within forward slashes.
datas.match(/a\+='(.*?)'/g);
OR
Escape the backslash one more time, if it's enclosed within double quotes.
> datas.match("a\\+='(.*?)'");
[ 'a+=\'00-2\'',
'00-2',
index: 15,
input: 'a=\'00-8\'b=\'13-\'a+=\'00-2\'b+=\'3333\'c=\'4\'' ]
> datas.match("a\\+='(.*?)'")[1];
'00-2'

Related

Trying to split string with escaped and non-escaped delimiter

I have a string with the form of a b/c\/d\/e/f. I'm trying to split the string on the non-escaped forward slashes.
I have this regex so far (?:[^\\/])/. However it consumes the last character preceding the /. So if I'm doing a replace with "#" instead of a split, the string looks like a #c\/d\/#f. In the case of the split, I get the strings separated the same with the last character being consumed.
I tried using a non capturing group but that doesn't seem to do the trick either. Doing this in javascript.
You may use this regex in JS to return you all the matches before / ignoring all escaped cases i.e. \/. This regex also takes care of the cases when \ is also escaped as \\.
/[^\\\/]*(?:\\.[^\\\/]*)*(?=\/|$)/gm
RegEx Demo
const regex = /[^\\\/]*(?:\\.[^\\\/]*)*(?=\/|$)/gm
const str = `\\\\\\\\\\\\/a b/c\\/d\\\\/e\\\\\\/f1/2\\\\\\\\\\\\\\/23/99`;
let m = str.match(regex).filter(Boolean)
console.log(m)
.filter(Boolean) is used to filter out empty matches.

replace single quote in middle of string using regex

Have a string:
stringName= "'john's example'"
Need to do a string.replace to remove the single quote in the middle of the string, not the first and last otherwise will break my javascript
have tried stringName.replace("/.'./","") to replace only the single quote in the middle of the string but does not work
Help is very appreciated! :)
Use (^'|'$)|' as matching regular expression:
stringName = "'john's e'xam'ple'";
console.log(
stringName.replace(/(^'|'$)|'/g, '$1')
);
First thing is you aren't doing a regex replace, you are replacing a string which looks like /.'./ (because of the " in the first argument). Secondly, the regex you're doing is only going to be looking for a single character (.) then a single quote, then another character. What you might want to do is something like stringName.replace(/(.+)'(.+)/, "$1$2")
Use split, join after stripping of first and last character
var f1 = (str) => str.charAt(0) + str.split("'").join("") + str.slice(-1);
f1( "'john's exa''mp'le'" ); //'johns example'

RegEx to replace variable quantity of characters with single character

Given this string
var d = 'The;Quick;;Brown;Fox;;;;;;Jumps';
What RegEx would I need to convert to this string:
'The,Quick,Brown,Fox,Jumps'
I need to replace 1-n characters (e.g. ';') with a single character (e.g. ',').
And because I know that sometimes you like to know "what are you trying to accomplish??"
I need to condition a string list of values that can be separated with a combination of different methods:
'The , Quick \r\n Brown , \r\n Fox ,Jumps,'
My approach was to convert all known delimiters to a standard character (e..g ';') and then replace that with the final desired ', ' delimiter
as Josh Crozier says, you can use
d = d.replace(/;+/g, ',');
You can also to the whole thing in one operation with something like
d = d.replace(/[,; \r\n]+/g, ',');
The [,; \r\n]+ part will find groups that are made of commas, semicolons, spaces etc.. Then the replace will replace them all with a single comma. You can add any other characters you want to treat as delimiters in with the brackets.
EDIT: actually, it's probably better to use something like this. The \s will match any whitespace character.
d.replace(/[,;\s]+/g, ',');
This should do the trick:
d.replace(/[;]+/g, ',')
It just replaces all group of semicolons together for a comma

Javascript regex replace single slash in to double slash?

Javascript regex replace single slash into double slash not for replace double slash in a string?
var tempPath ="//DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg/IMAGES/2008/20130411/16192144/16192144-10003.tif&";
Here replace all single slash in to double (//) not to all double slash.
like //DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg//IMAGES//2008//20130411//16192144//16192144-10003.tif&
This would work assuming your string does not also end in a /
yourString.replace(/\/[^\/]/g,"//")
/stuff/ is just JavaScript regex literal notation
\/ is an escaped "/"
[^\/] is anything but a "/" (again, with escaping)
the "g" on the regex literal means "replace all matches and not just the first"
which we replace for "//" which is what you want.
replace accepts a string and returns a new string with the value changed without changing the original.
Here is a working fiddle
yourString.replace(/([^\/])\/([^\/])/g,"$1//$2")
Could be also helpful:
var s = "http://www.some-url.com//path//to";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");

Replace '\' with '-' in a string

I have seen all the questions asked previously but it didn't help me out . I have a string that contains backslash and i want to replace the backslashes with '-'
var s="adbc\sjhf\fkjfh\af";
s = s.replace(/\\/g,'-');
alert(s);
I thought this is the proper way to do it and of course i am wrong because in alert it shows adbcsjhffkjfhaf but i need it to be like adbc-sjhf-fkjfh-af.
What mistake i do here and what is the reason for it and how to achieve this...??
Working JS Fiddle
Your s is initially adbcsjhffkjfhaf. You meant
var s="adbc\\sjhf\\fkjfh\\af";
You need to double-up the backslashes in your input string:
var s="adbc\\sjhf\\fkjfh\\af";
Prefixing a character with '\' in a string literal gives special meaning to that character (eg '\t' means a tab character). If you want to actually include a '\' in your string you must escape it with a second backslash: '\\'
Javascript is ignoring the \ in \s \f \a in your string. Do a console.log(s) after assigning, you will understand.
You need to escape \ with \\. Like: "adbc\\sjhf\\fkjfh\\af"
The string doesn't contain a backslash, it contains the \a, \s and \f (escape sequence for Form Feed).
if you change your string to adbc\\sjhf\\fkjfh\\af
var s="adbc\\sjhf\\fkjfh\\af";
s = s.replace(/\\/g,'-');
alert(s);
you will be able to replace it with -

Categories

Resources