I have sample string as :
'&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89&color=ff0000|00ffff'
'&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89'
My objective is to select the text from 'tooltext=' till the first occurrence of '&' which is not preceded by \\. I'm using the following regex :
/(tooltext=)(.*)([^\\])(&)/
and the .match() function.
It is working fine for the second string but for the first string it is selecting upto the last occurrence of '&' not preceded by \\.
var a = '&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89&color=ff0000|00ffff',
b = a.match(/(tooltext=)(.*)([^\\])(&)(.*[^\\]&)?/i)
result,
b = ["tooltext=abc\&|\|cba&value=40|84|40|62&", "tooltext=", "abc\&|\|cba&value=40|84|40|6", "2", "&"]
But what i need is:
b = ["tooltext=abc&||cba&", "tooltext=", "abc&||cb", "a", "&"]
I think you can use a regex like this:
/tooltext=(.*?\\&)*.*?&/
[Regex Demo]
and to always found a non-escaped &:
/tooltext=(.*?\\&)*.*?[^\\]&/
It looks like your regex is matching all the way to the last & instead of the first one without a backslash in front of it. Try adding the ? operator to make it match as small as possible like so:
/(tooltext=)(.*?)([^\\])(&)/
And if you just want the text between tooltext= and &, try this to make it return the important part as the matched group:
/tooltext=(.*?[^\\])&/
Related
Here is my string:
type_logistics[][delivery]
type_logistics[][random]
type_logistics[][word]
I would like to pull out the word, whatever it is, inside the second set of brackets. I thought that meant doing something like this:
Indicate that the start of the string I want to capture is [ by writing ^\[
Indicate that there will be any number 1+ of characters using [a-z]+
Indicate that the end will be ] by using \]$
The above three steps should get me to [delivery], [random], [word] in which case I'd just wrap the entire regex in a capture parenthesis ()
My finished statement would have been
string.match(/^\[([a-z]+)\]$/)
Have been playing with regex101.com and literally none of my assumptions have worked LOL. Please help?
With ^ you are assuming the String you are checking starts there. Your String starts with type_logistics and not as expected by the regex with a [
To detect the 2nd set of brackets you need to either add the type_logistics[] to the regex or just match everything before the 1st set of brackets with .*
When working with multiple lines (for example during testing on regex101), don't forget to set the modifiers gm
g modifier: global. All matches (don't return on first match) m modifier: multi-line. Causes ^ and $ to match the begin/end of each
line (not only begin/end of string)
These all would work for your test cases
/^.*\[\]\[([a-z]+)\]$/gm
/^type_logistics\[\]\[([a-z]+)\]$/gm
/^.*\[([a-z]+)\]$/gm
Match [ followed by a-z followed by ] , convert back to string, split [ character, filter "" empty string
var str = "type_logistics[][delivery] type_logistics[][random] type_logistics[][word]"
var res = str.match(/(\[[a-z]+)(?=\])/g).join("").split(/\[/).filter(Boolean);
console.log(res);
document.body.textContent = res;
I'm getting a string which looks like following
"{option:{name:angshu,title:guha}}"
Now I have to make a valid JSON string from this. Is there any smart way to convert that. I tried with string handelling but that takes a lot of conditions still being case specific. Even i tried with eval() that doesn't work also.
This regex will do the trick for the provided example string:
/:([^{},]+)/g
Regex101 analysis of it:
: matches the character : literally
1st Capturing group ([^{},]+)
[^{,}]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
{}, a single character in the list {}, literally
g modifier: global. All matches (don't return on first match)
Basically, it looks for all characters following a : that aren't {},. those "words" are saved in the 1st capturing group, which allows .replace to re-use them with $1.
You can use the regex like this:
var raw = "{option:{name:angshu,title:guha}}",
regex = /:([^{,}]+)/g,
replacement = ':"$1"';
var jsonString = raw.replace(regex, replacement);
alert(jsonString);
try this if you are looking for JSON object
string inputString = '{option:{name:angshu,title:guha}}';
var obj = JSON.parse(inputString);
My string can be something like A01, B02, C03, possibly AA18 in the future as well. I thought I could use a regex to get just the letters and work on my regex since I haven't done much with it. I wrote this function:
function rowOffset(sequence) {
console.log(sequence);
var matches = /^[a-zA-Z]+$/.exec(sequence);
console.log(matches);
var letter = matches[0].toUpperCase();
return letter;
}
var x = "A01";
console.log(rowOffset(x));
My matches continue to be null. Am I doing this correctly? Looking at this post, I thought the regex was correct: Regular expression for only characters a-z, A-Z
You can use String#replace to remove all non letters from input string:
var r = 'AA18'.replace(/[^a-zA-Z]+/g, '');
//=> "AA"
Your main issue is the use of the ^ and $ characters in the regex pattern. ^ indicates the beginning of the string and $ indicates the end, so you pattern is looking for a string that is ONLY a group of one or more letters, from the beginning to the end of the string.
Additionally, if you want to get each individual instance of the letters, you want to include the "global" indicator (g) at the end of your regex pattern: /[a-zA-Z]+/g. Leaving that out means that it will only find the first instance of the pattern and then stop searching . . . adding it will match all instances.
Those two updates should get you going.
EDIT:
Also, you may want to use match() rather than exec(). If you have a string of multiple values (e.g., "A01, B02, C03, AA18"), match() will return them all in an array, whereas, exec() will only match the first one. If it is only ever one value, then exec() will be fine (and you also wouldn't need the "global" flag).
If you want to use match(), you need to change your code order just a bit to:
var matches = sequence.match(/[a-zA-Z]+/g);
To return an array of separate letters remove +:
var matches = sequence.match(/[a-zA-Z]/g);
You're confused about what the goal of the other question was: he wanted to check that there were only letters in his string.
You need to remove the anchors ^$, who match respectively the beginning and end of the string:
[a-zA-Z]+
This will match the first of letters in your input string.
If there might be more (ie you want multiple matches in your single string), use
sequence.match(/[a-zA-Z]+/g)
This /[^a-z]/g solves the problem. Look at the example below.
function pangram(str) {
let regExp = /[^a-z]/g;
let letters = str.toLowerCase().replace(regExp, '');
document.getElementById('letters').innerHTML = letters;
}
pangram('GHV 2## %hfr efg uor7 489(*&^% knt lhtkjj ngnm!##$%^&*()_');
<h4 id="letters"></h4>
You can do this:
var r = 'AA18'.replace(/[\W\d_]/g, ''); // AA
Also can be done by String.prototype.split(regex).
'AA12BB34'.split(/(\d+)/); // ["AA", "12", "BB", "34", ""]
'AA12BB34'.split(/(\d+)/)[0]; // "AA"
Here regex divides the giving string by digits (\d+)
This
var re = /[^<a]b/;
var str = "<a>b";
console.log(str.match(re)[0]);
matches >b.
However, I don't understand why this pattern /[^<a>]b/ doesn't match anything. I want to capture only the "b".
The reason why /[^<a>]b/ doesn't do anything is that you are ignoring <, a, and > as individual characters, so rewriting it as /[^><a]b/ would do the same thing. I doubt this is what you want, though. Try the following:
var re = /<a>(b)/;
var str = "<a>b";
console.log(str.match(re)[1]);
This regex looks for a string that looks like <a>b first, but it captures the b with the parentheses. To access the b, simply use [1] when you call .match instead of [0], which would return the entire string (<a>b).
What you're using here is a match for a b preceded by any character that is not listed in the group. The syntax [^a-z+-] where the a-z+- is a range of characters (in this case, the range of the lowercase Latin letters, a plus sign and a minus sign). So, what your regex pattern matches is any b preceded by a character that is NOT < or a. Since > doesn't fall in that range, it matches it.
The range selector basically works the same as a list of characters that are seperated by OR pipes: [abcd] matches the same as (a|b|c|d). Range selectors just have an extra functionality of also matching that same string via [a-d], using a dash in between character ranges. Putting a ^ at the start of a range automatically turns this positive range selector into a negative one, so it will match anything BUT the characters in that range.
What you are looking for is a negative lookahead. Those can exclude something from matching longer strings. Those work in this format: (?!do not match) where do not match uses the normal regex syntax. In this case, you want to test if the preceding string does not match <a>, so just use:
(?!<a>)(.{3}|^.{0,2})b
That will match the b when it is either preceded by three characters that are not <a>, or by fewer characters that are at the start of the line.
PS: what you are probably looking for is the "negative lookbehind", which sadly isn't available in JavaScript regular expressions. The way that would work is (?<!<a>)b in other languages. Because JavaScript doesn't have negative lookbehinds, you'll have to use this alternative regex.
you could write a pattern to match anchor tag and then replace it with empty string
var str = "<a>b</a>";
str = str.replace(/((<a[\w\s=\[\]\'\"\-]*>)|</a>)/gi,'')
this will replace the following strings with 'b'
<a>b</a>
<a class='link-l3'>b</a>
to better get familiar with regEx patterns you may find this website very useful regExPal
Your code :
var re = /[^<a>]b/;
var str = "<a>b";
console.log(str.match(re));
Why [^<a>]b is not matching with anything ?
The meaning of [^<a>]b is any character except < or a or > then b .
Hear b is followed by > , so it will not match .
If you want to match b , then you need to give like this :
var re = /(?:[\<a\>])(b)/;
var str = "<a>b";
console.log(str.match(re)[1]);
DEMO And EXPLANATION
i want to replace last input character from keyboard to ''
My String Input are
sample string
"<p><strong>abscd sample text</strong></p>"
"<p>abscd sample text!</p>"
My last character is dynamic that can be any thing between
a to z, A to Z, 0 to 9, any special characters([~ / < > & ( . ] ).
So i need to replace just that character
for example in Sample 1 i need to replace "t" and in sample 2 in need to replace "!"
I tried below code. but it id not worked for me
var replace = '/'+somechar+'$/';
Any way to do it?
Step one
to replace the a character in a string, use replace() function of javaScript. Here is the MDN specification:
Returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.
Step two
you need to location the character to be replaced through regular expression. You want to replace the last character of a string and this could be expressed as /(.+)(.)$/. . stands for any character, + means more than one character. Here (.+) matches all the character before the last one. (.) matches the last character.
What you want to replace is the one inside the second brackets. Thus you use the same string matched in the first bracket with $1 and replace whatever after it.
Here is the code to realize your intention:
text = 'abscd sample text';
text.replace(/(.+)(.)$/, '$1!');
Do you really need to use regular expressions? How about str = str.slice(0, -1); ? This will remove the last character.
If you need to replace a specific character, do it like this:
var replace = new RegExp(somechar + '$');
str = str.replace(replace, '');
You cannot use slashes in a string to construct a RegEx. This is different from PHP, for example.
I dont really understand which character you want to replace to what, but i think, you should use replace() function in JS: http://w3schools.com/jsref/jsref_replace.asp
string.replace(regexp/substr,newstring)
This means all keyboard character:
[\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]
And this way you can replace all keyboard character before < mark to ""
string.replace("[a-zA-Z0-9\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]<","<")