Neler yeni
Türkiye'nin En Güncel Forum Sitesi

Forum içeriğine ve tüm hizmetlerimize erişim sağlamak için foruma kayıt olmalı yada giriş yapmalısınız. Forum üye olmak tamamen ücretsizdir.

Metin düzenleyici


Kayıtlı Üye
MFC Üyesi
  • Üyelik Tarihi
    7 Ocak 2015
  • Mesajlar
  • MFC Puanı
  • MFC Seviyesi

<title>Find and Replace using split() and join()</title>

Find and Replace using split() and join()
by pile o' nades

This script will find and replace 

function FindReplace(find,replaceWith){
  // get value of find and replaceWith textboxes
  find = ********.getElementById(find).value;
  replaceWith = ********.getElementById(replaceWith).value;

  // get HTML of search area
  var result = ********.getElementById("searchField").innerHTML;

  // split the search area into array items
  result = result.split(find);

  // get number of items while result is still an array
  var numberOfItems = result.length-1;

  // join the array items back into a string using replaceWith
  result = result.join(replaceWith);

  // rewrite the search area with the new text
  ********.getElementById("searchField").innerHTML = result;

  // write number of items to the number textbox
  ********.getElementById("number").value = numberOfItems;



<h1 align="center">Find and Replace using split() and join()</h1>

<div style="width:400">
You have probably seen other find and replace scripts that use Range or
TextRange objects to find and replace text, or indexOf() and substring()
to manipulate the text, but I have found a third way to do that, which is
via the split() and join() methods.

Normally, the split() and join() methods are used to combine arrays into 
strings, to be stored in cookies. But they can also be used for a find and 
replace script, such as this one.

The split() method takes a peice of text and/or HTML, and makes each peice 
of text before and after each occurance of the text into an array item.

var text = "Congress shall make no law respecting an establishment of religion";
text = text.split("a")

This would equal:
var text = new Array();
text[0]="Congress shall make no l";
text[1]="w respecting ";
text[2]="n est";
text[3]="blishment of religion";

all of the "a"s have been removed 

The join() method does the opposite, it recontructs the array into a string
text = text.join("a")

This would equal:
var text = "Congress shall make no law respecting an establishment of religion";

However, you can use a different character for join()

text = text.join("117")

This would equal:
var text = "Congress shall make no l117w respecting 117n est117blishment of religion";

That's how this script works. Try it using the form below.
<p align="right">
Find:<input id="find" value="law"><br />
Replace with:<input id="replaceWith" value="legislation"><br />
Number of items:<input id="number"><br />

<button id="button" onclick="FindReplace('find','replaceWith')">Go!</button>


<div id="searchField">
    <!-- Bill of Rights -->
    <h2><a id="article1" name="article1">ARTICLE I</a></h2>
    <p>Congress shall make no law respecting an establishment of religion, or
      prohibiting the free exercise thereof; or abridging the freedom of
      speech, or of the press; or the right of the people peaceably to
      assemble, and to petition the government for a redress of grievances.</p>
    <h2><a id="article2" name="article2">ARTICLE II</a></h2>
    <p>A well regulated militia, being necessary to the security of a free
      state, the right of the people to keep and bear arms, shall not be
    <h2><a id="article3" name="article3">ARTICLE III</a></h2>
    <p>No soldier shall, in time of peace, be quartered in any house, without
      the consent of the owner, nor in time of war, but in in a manner to be
      prescribed by law.</p>
    <h2><a id="article4" name="article4">ARTICLE IV</a></h2>
    <p>The right of the people to be secure in their persons, houses, papers,
      and effects, against unreasonable searches and seizures shall not be
      violated, and no warrants shall issue, but upon probable cause,
      supported by oath or affirmation, and particularly describing the
      place to be searched, and the persons or things to be seized.</p>
    <h2><a id="article5" name="article5">ARTICLE V</a></h2>
    <p>No person shall be held to answer for a capital, or otherwise infamous
      crime, unless on a presentment or indictment of a grand jury, except
      in cases arising in the land or naval forces, or in the militia, when
      in actual service in time of war or public danger; nor shall any
      person be subject for the same offense to be twice put in jeopardy of
      life or limb; nor shall be compelled in any criminal case to be a
      witness against himself, nor be deprived of life, liberty, or
      property, without due process of law; nor shall private property be
      taken for public use, without just compensation.</p>
    <h2><a id="article6" name="article6">ARTICLE VI</a></h2>
    <p>In all criminal prosecutions the accused shall enjoy the right to a
      speedy and public trial, by an impartial jury of the state and
      district wherein the crime shall have been committed, which district
      shall have been previously ascertained by law, and to be informed of
      the nature and cause of the accusation; to be confronted with the
      witnesses against him; to have compulsory process for obtaining
      witnesses in his favor, and to have the assistance of counsel for his
    <h2><a id="article7" name="article7">ARTICLE VII</a></h2>
    <p>In suits at common law, where the value in controversy shall exceed
      twenty dollars, the right of trial by jury shall be preserved, and no
      fact tried by a jury shall be otherwise re-examined in any court of
      the United States, than according to the rules of the common law.</p>
    <h2><a id="article8" name="article8">ARTICLE VIII</a></h2>
    <p>Excessive bail shall not be required, nor excessive fines imposed, nor
      cruel and unusual punishments inflicted.</p>
    <h2><a id="article9" name="article9">ARTICLE IX</a></h2>
    <p>The enumeration in the Constitution, of certain rights, shall not be
      construed to deny or disparage others retained by the people.</p>
    <h2><a id="article10" name="article10">ARTICLE X</a></h2>
    <p>The powers not delegated to the United States by the Constitution, nor
      prohibited by it to the states, are reserved to the states
      respectively, or the the people.</p>
Üst Alt