Regular expression failed replace - javascript

I want to use Regular expression to replace this text :
[Button size="Big" color="#000"] test [/Button]
to Button, I used this site http://www.freeformatter.com/regex-tester.html but not working the replace.
the Regular expression \[Button([^\]]*)\[/Button],it give me the result String is same as before replace! what the mistake?

The ([^\]]*) part of your regex will stop matching before the closing ] of first tag. So, you don't have the pattern to match string - "] test " thereafter.
Modify your regex to:
\[Button([^\]]*][^\[]*)\[/Button]

Try with this, it matches properly and also matches the insides.
\[Button([^\]]*)\](.*?)\[/Button\]

The regex of
\[Button.*\]
and the replacement string of Button should be enough.

Related

how to remove just one character in a string of javascript

#a:{width:100px;height:100px;background-color:black;}#b:{width:100px;}
i have the above string
i want that the character: only after css selector like #a and #b get removed from this string
i thought that i must use regular expressions so i wrote one:
/[#\.A-Za-z0-9]+([:])[{]/g
see this regular expression working on regex101
but you know it matches : but when i try to remove this using replace method then whole #a:{ and #b:{ get removed
any help would be great!
The regex is almost correct. What you need to do is to repalce the with $1$2 instead of null string
Also make a small change to the regex as
/([#.A-Za-z0-9]+):({)/g
Regex Example
Changes made
([#.A-Za-z0-9]+) enclosed in brackets. The matched string is captured in $1 hence for the frist match $1 will contain #a
Within a character class its not required to escape the . as it looses it meaning in the class.
[{] to ({) The [] surrounding does not make any difference, hence drop it. Enclosed in (), hence captured in $2, for example in first match the $2 will contian {
Replace string $1$2
will give output as
#a{width:100px;height:100px;background-color:black;} #b{width:100px;}
Javascript
var value = "#a:{width:100px;height:100px;background-color:black;}#b:{width:100px;}";
alert(value.replace(/(#.):/g, "$1"));
Example: http://jsfiddle.net/7hs0jgd2/

Regular Expression Failure javascript

Is there any reason the following string should fail the regular expression below?
String: "http://devices/"
Expression:
/^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)/.test(input.val())
Thank you for your consideration.
Yes it will fail because of the last dot . in your regular expression.
/^ ... \.)/
^^
There is not one in the string you are validating against.
http://devices
^ Should be a dot, not a forward slash
Live Demo
If you are planning on using regex to do this, I would probably prefer using a RegExp Object to avoid all the escaping, or group the prefixes together using a non-capturing group.
/^((?:https?|ftp|pop|imap):\/{2}|www\.) ... $/
The last character in the string must be a period. see "\." at the end of the regex.
You can use http://rubular.com/ to test simple regex expressions and what their matches are.
The reason why it's failing is because, you are using:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)
and you should use:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+.)
You don't have to escape . --------^
You need to close the regex with a $.
On this two last: .), this dot should be optional, as it is needed to validade.
to satisfy this "http://devices/" the regex in java at least is:
^((http://)|(https://)|(ftp://)|(pop://)|(imap://)){1}(www.)?([0-9A-Za-z]+)(.)?([0-9A-Za-z]+)?$
Are those / at the beggining and the end code delimiters?

javascript replace function only replaces the first occurance

document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace("'","%%");
The above statement replaces only the first occurrence of the single quote. Could it be because that I do a submit right after that, and that javascript doesn't wait for the previous statement to be completed before moving on to the next one?
Try using regex /g
.replace(/'/g,"%%")
Change your code as below,
document.getElementById("Message").innerHTML =
document.getElementById("Message")
.innerHTML
.replace(/'/g,"%%");
To replace globally in javascript you need to add /g to the replacement string.
See this SO link How to replace all dots in a string using JavaScript
You should use regular expression in replace.
document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace(/'/g,"%%");
jsfiddle
Use the Global (g) flag in the replace() parameters.
See here
You should use a regular expression and the /g (global) flag. This will replace all occurrences:
document.getElementById("Message").innerHTML=
document.getElementById("Message").innerHTML.replace(/'/g,"%%");
http://www.w3schools.com/jsref/jsref_replace.asp

Javascript replace function clarification

I am trying to learn javascript
i have this code:
x=x.replace(/^\s+|\s+$/g,"");
can i have a description about /^\s+|\s+$/g,""
searching to replace what with what ?
Regards
This is a regular expression. Basically \s matches whitespac characters and replaces them with "".
edit:
/ ... / marks the regex.
^\s+ Take 1or more whitespaces at the beginning of the string.
\s+$ Take 1 or more whitespaces at the end of the string.
/g Dont stop at the first match, but find all matches - "global flag"
It's removing whitespaces, either at the beginning or the end of the string, by replacing them with the empty string "".
You can find more about regular expressions here http://www.w3schools.com/jsref/jsref_obj_regexp.asp.
first, look at this: http://www.w3schools.com/jsref/jsref_replace.asp and then do not use stackoverflow for questions of this kind

How to remove periods in a string using jQuery

I have the string R.E.M. and I need to make it REM
So far, I have:
$('#request_artist').val().replace(".", "");
...but I get RE.M.
Any ideas?
The first argument to replace() is usually a regular expression.
Use the global modifier:
$('#request_artist').val().replace(/\./g, "");
replace() at MDC
You could pass a regular expression to the replace method and indicate that it should replace all occurrences like this: $('#request_artist').val().replace(/\./g, '');
The method used to replace the string is not recursive, meaning once it found a matching char or string, it stop looking. U should use a regular expression replace. $("#request_artist").val().replace(/\./g, ''); Check out Javascript replace tutorial for more info.

Categories