JS/Jquery/RegEx - Remove all tags except the ones with classname XYZ - regex

this is driving me nuts ;-) I have a string whith various span tags... I want to remove all span tags except the ones with classname XYZ... The problem is that i havent found a solution to leave the closing tag...
My starting point is this regex:
text = text.replace(/<\/?[^>]+(>|$)/g, "");
But everything i tried to say "DONT DO IT IF MATCH classnameXYZ Failed till now...
Any ideas? Thank you in advance!

Ok, this works for my needs ;-)
$('#text > span').each(function(intIndex){
var word;
if ($(this).hasClass('checked')) {
word = "<span>"+$(this).html()+"</span>";
} else {
word = $(this).html();
word = word.replace(/<\/?[^>]+(>|$)/g, "");
}
console.log(word);
});

This can be done with out a regular expression, more over your answer need to cache the entire html, which would be slow, try the below code, It may help :)
$(function()
$('#text > span').each(function() {
if(!$(this).hasClass('XYZ')) {
$(this).remove();
}
});
});

Related

Full name regex expression not working

Im trying to use a regex expression to validate a full name for a textfield form in flutter but i cant figure out why it isnt working.
The expression i found from here /^[a-z ,.'-]+$/i is failing for any entry i give.
The code i'm using in dart for my flutter app is:
final RegExp nameExp = new RegExp(r"/^[a-z ,.'-]+$", caseSensitive: false);
Can anyone see what i'm missing?
Update: so the regex was allowing most things and wasnt quite correct, what worked for me was r"^([a-zA-Z]{2,}\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?)" with the accepted answer below
Remove slash / delimiters and add the Dart delimiters : r'^[a-z ,.'-]+$ '
use below method for validate full name
String fullNameValidate(String fullName) {
String patttern = r'^[a-z A-Z,.\-]+$';
RegExp regExp = new RegExp(patttern);
if (fullName.length == 0) {
return 'Please enter full name';
} else if (!regExp.hasMatch(fullName)) {
return 'Please enter valid full name';
}
return null;
}
Pattern namePattern = r"^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$"
RegExp nameRegex = RegExp(namePattern, caseSensitive: false);

Regex - grab everything between tag (including the tag)

I've been battling a couple things all day, and my brain is no longer functioning.
I am using this as a string "something <strong>here</strong>" and trying t ograb this out of it: <strong>here</strong>
How can I do this? I thought string.scan(/(?<=)(.*?)(=</strong>)/) would work but apparently not. Doing this in ruby btw.
<TAG\b[^>]*>(.*?)</TAG>
Is the problem that you're not escaping the slash in ?
1.9.3-p547 :012 > my_string = "something <strong>here</strong> something else <strong>another thing</strong>"
=> "something <strong>here</strong> something else <strong>another thing</strong>"
1.9.3-p547 :013 > my_string.scan(/(<strong>)(.*?)(<\/strong>)/)
=> [["<strong>", "here", "</strong>"], ["<strong>", "another thing", "</strong>"]]
Edit: This grabs it into a single element.
my_string.scan(/<strong>.*?<\/strong>/)

Using curly braces in JavaScript in Play Framework template

I have passed a list of titles that I have passed through from my controller:
#titles:List[String]
I want to cycle through generating some html headings with {mmm} appearing after:
#titles.map { title =>
<h1>{title} {mmm}</h1>
}
Clearly there is a problem as it will try and find a variable called mmm. How do I escape the brackets?
Or is there a more idiomatic way to generate the HTML that doesn't involve mapping the list? Very new to this!
You shouldn't need to escape curly brackets in Play—unlike in Scala's XML literals, they don't have any special meaning in Play templates except after an #.
You should be able to write the following:
#titles.map { title =>
<h1>#{title} {mmm}</h1>
}
And get <h1>Whatever Title {mmm}</hq>, etc.
Travis' answer demonstrates one possibility (for that you asked for) and it's correct. On the other hand you can also meet other cases and you need to keep in mind other tricks:
for an example this will fail:
#for(title <- titles){
Title: #title {mmm} <br/>
}
But this will work (thanks Travis)
#for(title <- titles) {
Title: #{title} {mmm} <br/>
}
And this will work as well
#for(title <- titles){
Title: #title - <b>something else without bracets</b> <br/>
}
Alternative
Alternatively you can create for an example toBrackets() method in your model and call it in your template (Java sample)
public String toBrackets(String string) {
return "{" + string + "}";
}
in template:
#for(title <- titles){
Title: #title #title.toBrackets("mmm") <br/>
}
If it is not part of JS or other code, and used just to present in the view, then we can use HTML entitles
{ for left curly brace
} for right curly brace
The result:
{ for left curly brace
} for right curly brace
You could create variables #LeftBracket = { and #RightBracket = }, and then this should work:
#titles.map { title =>
<h1>{title} #{LeftBracket}mmm#RightBracket</h1>
}
A side note somewhat related to your question:
My impression is that unless the brackets are matched (that is, a } for each {),
template compilation fails, so perhaps #RightBracket is the only way to insert a single }? This'd be useful in case you needed to write:
#titles.map { title =>
hello :-}
}
:-}

Regex pattern matching (Javascript/ASP)

I need some help with a regular expression, please help if you can
I have the following code: I am using Javascript and ASP
{In|inside|during|into|in the sphere
of} {this} {article|piece of
writing|editorial|commentary|paragraph|section}
{we} {will|desire to|wishto|want
to|resolve to|will} {tell} {you}
{more} {about|regarding|with reference
to} {the}
The desired code should look like this:
{In|inside|during|into|in the sphere
of} this {article|piece of
writing|editorial|commentary|paragraph|section}
we {will|desire to|wishto|want
to|resolve to|will} tell you
more {about|regarding|with reference
to} the
The brackets around the single words with no | should be removed like - this - we - tell you more - the in the example above.
I am thinking that the solution should be something like this
replace(/{.+?[^\|]/ig, '');
to replace the { there should not be a | in the code; {.+?[^\|] and replace { with nothing
Then if there is not a starting { to replace the } with nothing
I am not sure how to do this, and how to only remove the {} where there is no | inside without removing the content...
x.replace(/{([^|}]*)}/g, '$1')
Try:
var string = "{hello|there} {yes} {no|me} {ok}";
string = string.replace(/{[A-Za-z0-9]+)}/g, "$1");
Gives you:
{hello|there} yes {no|me} ok

How do I linkify text using ActionScript 3

I have a text that I want to linkify (identify URLs and convert them to HTML links). The text could be multi-line, and could contain multiple urls like the example below.
My current actionscript code looks like this
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function init():void {
var str:String = "#stack the website for google is http://www.google.com and gmail is http://gmail.com";
//Alert.show(linkify(str),"Error");
txtStatus.htmlText = linkify(str);
}
private function linkify(texty:String):String {
//return texty.replace("/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g",function(m):String { return m.linkify(m);});
//return texty.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m):String {return m.linkify(m);}).replace(/(^|[^\w])(#[\d\w\-]+)/g, function(m2):String{return '#' + m2.substr(1) + ''; });
var pattern:RegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g;
var match:String = pattern.exec(texty);
return texty.replace(pattern,'<a href="' + match + '">' +
match + '</a>');
}
]]>
</mx:Script>
The problem with the above script is that it recognizes the first match and uses that across. Also how do i do it for #?
Any help is highly appreciated.
ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...
well, anyway, have a look at AS3 regex metacharacters ...
that'll GREATLY improve your expression's readability and is much more robust...
i'd go with something like this, really:
var r:RegExp = /(?:http|https):\/\/\S*/g;
trace(str.replace(r, function (s:String,...rest):String {
return '' + s + ''
} ));
but the actual point, was the global flag ...
good luck then ... :)
greetz
back2dos