regex replace Blocks start and end - javascript

I'm trying have a source to generate two source, for web and phone, I'd like to write a source js like this:
{*Phone
import webfunction from ./utils
*/Phone}
...anothercode..
{*Web
import webfunction from ./utils
*/Web}
...anothercode..
I've heard that with regex is not easy to replace blocks, inside de blocks will no write comments or strange chars, but i'dont know what it's the better strings that I can use with regex to get easier this.
If I want to get the pure parte for Phone and save to a new file; i've just call regex function to delete Web part, and vice versa ...
anyone does any idea what is the best string that i can use to achieve this ?
Update 1:
In this case the regex is not working, with two blocks inside ddd
{*Web
dddd
}
ddd
{*Web
*}

You can use the following regex:
\{\*(\w+)[\s\S]*?\*\/\1\}
And test it here

Related

Email regex string # symbol not accepted

I tried to email validate in function, but regex string couldnt allowed. My code is below:
function validateEmail(email) {
var re = /^\S+#\S+\.\S+$/;
return re.test(email);
}
# symbol give me a warning. this code in script tags.
For regex, certain characters will act as things you don't want them to.
Say for example, you were starting a capturing group. To do that, you would put parentheses around the text. What if you didn't want to open a capturing group, though?
That's where the backslash comes in. Simply use a backslash before the ( and the group will be gone.
Now, you can also do this with other symbols, too. Stuff like \ and / need to be escaped when searching for them, so you can apply that logic here. You need to use \#.
Your new code would be:
function validateEmail(email) {
var re = /^\S+\#\S+\.\S+$/;
return re.test(email);
}
console.log(validateEmail("email#example.com"));
console.log(validateEmail("fake email"));
Although this code does work, might I suggest using PHP, NodeJS, or other server-side languages to validate emails? Validating in the client will make your website very prone to getting fake emails. This is because you can simply go into Inspect Element and remove that line of code very easily. If I were to copy some code from the source, and then run it, then did some tinkering to make sure the code works, you wouldn't be able to stop me.
Anyhow, I've tested my answer, and I believe it does work.

Javascript - regex to check if user write correct formated input

in my CLI users can specify what they want to use:
A user command can look like this:
include=name1,name2,name3
category=name1,name2
category=name1
In another words, a command always consists of 3 parts:
command name: can be just include or category
=: is in every command
name or names of things they want to use, split by ,
How can I test this to get always true but false on everything else.
I am really bad in regex but I tried something like this:
/\category|include=\w/.test(str);
to simply test, at least, the most easy alternative which would be category=name1 but without success.
Can someone help me with this?
You were on the right path. Here's a fixed regex:
/^(category|include)=\w+(,\w+)*$/.test(str);
Note:
the parens around the alternative parts
the + after the \w so that you can have several characters
the optional (,\w+)*
the start and end of string marks (^ and $) in order to check the whole string
You can use this regex for your requorement:
/^(category|include)=(\w+(?:,\w+)*)$/
RegEx Demo
\w+(?:,\w+)*) in the value part after = will allow 1 or more of comma separated words.

Understand code from string using Regex or something - Js

I wanted to run a string replace function on a piece of code and make sure that all the strings in the code is intact and unchanged using javascript. For example if I have a code like below:
var a = "I am ok";
if (a == "I am ok") {
alert("That's great to know");
}
Now, I want to run a string replace on this code block. But it should only effect the code part of it. Not the strings which are in double quotes. Can this be done using regex or any other method?
AST
To avoid any chance of error in code manipulation using an Abstract Syntax Tree (AST) type solution is best. One example implementation is in UglifyJS2 which is a JavaScript parser, minifier, compressor or beautifier toolkit.
RegEx
Alternatively if an AST is over the top for your specific task you can use RegEx.
But do you have to contend with comments too?
The process might look like this:
Use a carefully formed regex to split the JavaScript code string based on these in this order:
comment blocks
comment lines
quoted strings both single and double quotes (remembering to contend with escaping of characters).
Iterate though the split components. If string (beings with " or ') or comment (begins with // or /*) ignore, otherwise run your replacement.
(and the simple part) join array of strings back together.
You would have to place the function code in a string variable, run a normal regex operation over that string, and then convert it to a function afterwards with:
var func = new Function('a', 'b', 'return a + b');
EDIT: Use regex to exclude the text between double quotes if you need to.

Hashtag linking in AngularJS and JS

I'm new to regex expressions and don't really understand them. I'm getting comments from a PHP script that may or may not include hashtags. I need to create a link out of the hashtag (not including urls or if the hashtag has a commas or a space in it)
So far I've looked online and found this:
string = string.replace(/(^|\s)(#[a-z\d-]+)/ig, "$1$2");
However, the link generated is:
#thenameofhashtag
I need to be able to exclude the hashtag from the tag= variable line. How can I modify the expression to achieve this and are there any angularJS way's of doing this? Additionally, are languages (Chinese, Japanese, etc) or characters that are not in UTF-8 encoded create problems?
You can exclude the # from the capturing group so that it is not captured in $2 as
(^|\s)#([a-z\d-]+)/ig
#([a-z\d-]+) Here the # is moved outside so that only [a-z\d-]+ is captured
Example
string.replace(/(^|\s)#([a-z\d-]+)/ig, "$1#$2");
// => #thenameofhashtag

JavaScript Regular Expression to find PHP tags

I have googled for a regex to grab and also read a few tutorials and I can't seem to get a solid regular expression to do this.
What I need to do is write a JavaScript regular expression that will match PHP tags within a string.
I have inherited a large php project where the views that have mixed html and php are not very readable. So what I'm doing is writing an IDE extension for my own personal use to strip out php that is mixed in a php view in order to run an HTML indentation script on it without confusing the HTML indentation script. Then after the indentation script has finished I go back and re-insert the php again.
What I have so far is this (I converted this from a regex that looks for brackets [], I knew it wouldn't match everything but it got me far enough to flesh out my IDE extension):
var php_tag_pattern = /<\?[^<>]*\?>/;
Now for obvious reasons it is not matching on code like this:
<?=$common->format_number($acct_info['number'])?>
or this:
<?
$wifi = $wifi_lib->wifi_radius($v['radiusgroupname']);
if (!empty($wifi)) :?>
I have been messing around with this for the last several hours so I thought I would finally ask for help to see what I'm missing.
Thanks!
To match multi-line php text you will need an implementation of DOTALL in Javascript. Unfortunately Javascript doesn't have s flag but there is a workaround. Try this code:
var php_tag_pattern = /<\?[=|php]?[\s\S]*?\?>/;
[\s\S] will make sure to match php text in multi line including new lines as well.
var php_tag_pattern = /<\?=?.+?\?>/;
var regex = /<\?[=|php]?[^<>]*\?>/;
You will also need to use the multiline modifier new RegExp(regex, "gim"). The 'g' is global, 'i' is case insensitive and 'm' multiline.

Categories

Resources