473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about match or replace

15 Byte
in php we have

[PHP]
<?php>
$pattern[0]='/a/i';
$pattern[1]='/b/i';
$replacement[0]='';
$replacement[1]='';
$var = 'Watbe';
echo preg_replace($pattern, $replacement, $var);
?>[/PHP]

using pattern array to replace two match at the same time.


Under is what i know in javascript
[HTML]<script>
var str="aacc";
var rs=str.replace(/a/i,"0");
</script>[/HTML]

Is there any patterns array for us to use like the php one???
Jul 9 '08 #1
2 944
hsriat
1,654 Expert 1GB
I'm afraid, there isn't anything like that.

Instead, you may do it like this:
Expand|Select|Wrap|Line Numbers
  1. string.replace(srch[0], rplc[0]).replace(srch[1], rplc[1]);//and so on... 
Or you can create a function similar to preg_replace of PHP which will iterate for each string in the array and repeat the replace() function accordingly.
Jul 9 '08 #2
rnd me
427 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. String.prototype.replaceAll = function (r) {
  2.     O = this;
  3.     for (z = 0; z < r.length; z++) {
  4.         tre = new RegExp(r[z][0], "gm");
  5.         O = O.replace(tre, r[z][1]);
  6.     }
  7.     return O;
  8. }
  9.  
  10. replacements = [
  11.    ["hello","goodbye"], 
  12.    ["world", "earth"]
  13. ]; //orig  //replacement
  14.  
  15. alert("hello world".replaceAll(replacements ));  //shows "goodbye earth"
  16.  
  17.  
  18.  
Jul 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
2
by: ommadawn | last post by:
Could someone tell me what's wrong with this code? It's supposed to produce an alert box with "George" replacing "Paul" and "Paula", but I'm only replacing the first name. var myString = "Paul,...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
5
by: alkapin | last post by:
I am trying to clean up a city name. Inside the letters only one of 3 characters (dot, space and hyphen) is allowed (1 max). For example: Los-Angeles,Los Angeles and N.Westminster are ok. Outside...
1
by: George Durzi | last post by:
Consider this excerpt from some HTML. (This is a copy from View->Source, except for the comment) <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0> <?xml version="1.0" encoding="UTF-16"?>...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yaşarsan yaşa sevdiğin kadardır...
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
4
by: looping | last post by:
Hi, It's not really a Python question but I'm sure someone could help me. When I use RE, I always have trouble with this kind of search: Ex. I've a text file: """ create or replace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.