I'm trying to make a RegEx that can single out words from a string but ignore them if they're inside a tag. For example: Even though the searchword is SPAN, do not replace a span tag.
What I have so far is:
(?<![<\/])\bspan\b(?!>)
http://regex101.com/r/vS6yG6
Span obviously is a placeholder. In the script it is generated from a dictionary dynamically.
This is what I'm trying to run:
var reg = new RegExp(the expression, 'gi');
I've escaped the /, so I'm not sure where the problem is.
And this is what I get back: SCRIPT5018: Unexpected quantifier
Any help would be appreciated. I made the Regular Expression with the help of regex101.com.
Try this ...
/>[^<>]*\b(span)\b[^<>]*<?/ig
Like David replied, there is no Negative LookBehind in Javascript, so that's where the problem was.
http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
Related
It drives me crazy to get the correct regex, can any one help, much appreciated.
Source String:
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
Result string needed :
<checklist></checklist>
<checklist></checklist>
Basically I need to get rid of the content in between pair (no class attribute).
I tried regex something like this
"/[^(.?)[^]*/g" using phone editing , if you can see this correctly , please see the regex I included in the comment
it didn't work, i am fairly new to regex
The following code snippet can repeat multiple times in the source string:
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
If you insist on a solution with regular expressions, you could do sth. like:
var string = '<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>';
var regex = /<checklist\s+[^>]+>.*?<\/checklist>/gi;
// that is, look for a checklist tag with additional attributes
// match everything up to a new closing tag (non-greedy)
// followed by a closing tag
var strippedString = string.replace(regex, '');
alert(strippedString);
See a JS fiddle here and a regex101 demo here.
EDIT: Added /g as #Atri pointed out.
Otherwise, consider using either document.getElementById or some other DOM function.
One friend asked me this and as my knowledge on RegExp is not so good yet here I am.
How can exclude the HTML tags from this string?
re<br>na<br>to<br>galvao
I've tried some RegExp but it didn't work as I was expecting.
(.*)<.*>(.*)
But this RegExp gets the first < and the last >.
Any ideas?
this is a quick way to do it:
var content = "re<br>na<br>to<br>galvao";
content = content.replace(/<[^>]*>/g,'');
You could use a non-greedy match. According to the answer to this question, in javascript it is *?
So, assuming this is the only problem with your regex, it should work with
(.*?)<.*?>(.*?)
Match all html tags with this regex:
<("[^"]*?"|'[^']*?'|[^'">])*>
see demo here: http://regex101.com/r/fA0oT4
I'm stuck. I tried it with regular expressions, but I guess I'm missing something. I'm working with JavaScript.
I have an input like:
(text [number]) the text that follows...
I want an output like:
[number] the text that follows...
I tried it with substr, but my problem is that I do not know the length of the text or number in the brackets. I guess I need the position of the beginning and ending of the number to work with a regEx.
Have you got an idea?
Regexes are the way to go — using JavaScript’s replace function, you don’t need to fiddle with the position of the number in the string.
Try this:
var geoff = '(text 694) the text that follows...';
var geoff_replaced = geoff.replace(/\([^0-9]* ([0-9]*)\)/, '$1');
# geoff_replaced will be "694 the text that follows...
I don’t do much JavaScript regex stuff, so I totally looked up the above on this guide to JavaScript regexes:
http://www.evolt.org/node/36435
It'd help to have a real example but I made one up...
Text:
(Some text 1234) some more text.
Regex:
^.+?(?<Number>\d+)\)(?<Text>.+)$
Replacement:
${Number}${Text}
Full example:
var fixedText = "(Some text 1234) some more text.".replace(/^.+?(?<Number>\d+)\)(?<Text>.+)$/, "${Number}${Text}");
the regex that matches (text [number]) the text that follows... can be like:
"^\(.*?([0-9]*)\)(.*)$"
or you can just match the beginning (and the ending )) and remove it
"^(\(.*?)[0-9]*(\)).*$"
$('#customerAddress').text().replace(/\xA0/,"").replace(/\s+/," ");
Going after the value in a span (id=customerAddress) and I'd like to reduce all sections of whitespace to a single whitespace. The /\s+/ whould work except this app gets some character 160's between street address and state/zip
What is a better way to write this? this does not currently work.
UPDATE:
I have figured out that
$('.customerAddress').text().replace(/\s+/g," ");
clears the 160s and the spaces.
But how would I write a regex to just go after the 160s?
$('.customerAddress').text().replace(String.fromCharCode(160)," ");
didn't even work.
Note: I'm testing in Firefox / Firebug
Regarding just replacing char 160, you forgot to make a global regex, so you are only replacing the first match. Try this:
$('.customerAddress').text()
.replace(new RegExp(String.fromCharCode(160),"g")," ");
Or even simpler, use your Hex example in your question with the global flag
$('.customerAddress').text().replace(/\xA0/g," ");
\s does already contain the character U+00A0:
[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
But you should add the g modifier to replace globally:
$('#customerAddress').text().replace(/\s+/g, " ")
Otherwise only the first match will be replaced.
Sorry if I'm being obvious (or wrong), but doesn't .text() when called w/o parameters just RETURNS the text? I mean, I don't know if you included the full code or just an excerpt, but to really replace the span you should do it like:
var t = $('#customerAddress').text().replace(/\xA0/,"").replace(/\s+/," ");
$('#customerAddress').text(t);
Other than that, the regex for collapsing the spaces seems OK, I'm just not sure about the syntax of your non-printable char there.
I would like to write a regular expression in javascript to match specific text, only when it is not part of an html link, i.e.
match match text
would not be matched, but
match text
or
<p>match text</p>
would be matched.
(The "match text" will change each time the search is run - I will use something like
var tmpStr = new RegExp("\bmatch text\b","g");
where the value of "match text" is read from a database.)
So far my best effort at a regular expression is
\bmatch text\b(?!</a>)
This deals with the closing , but not the initial . This will probably work fine for my purposes, but it does not seem ideal. I'd appreciate any help with refining the regular expression.
You can use a negative look-behind to get the opening <a href=...:
var tmpStr = new RegExp('(?<!<a.*?>)match text(?!</a>)');
Hope that works for you.
Thanks for the very quick and helpful answers. Just to clarify, the regular expression I ended up using was
(?!<a.*?>)\bmatch text\b(?!</a>)